小算法记录
小算法记录
zhuqianli 发表于3个月前
小算法记录
  • 发表于 3个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

write a program that input a positive integer, find out all the prime factors of it. the prime factors of 72 is 2,3 Example 72 =2^3* 3^2 181944 =2^3 *3^2*7*19^2 the prime factors of181944 is 2,3,7,19

public class PrimeAlgorithm {
    public static List factor(long g) {
        List result = new ArrayList(16);
        for (long i = 2; g != 1; i++)
            if (g % i == 0) {
                g /= i;
                result.add(i);
                i--; // i--使得i的值不变,即能把含有的所有的当前质因数, 如 2,除尽后, 再去除下一个指数, 如 3
            }

        return result;
    }

    public static void main(String[] args) {
        List factor = PrimeAlgorithm.factor(2132123);
        System.out.println(factor);
    }
}

 

一段生成分布式全局唯一ID的代码

https://gist.github.com/longkerdandy/a296b7a084fd191af6e4176eed168812


public class IdGenerator {

  // Modeled after base64 web-safe chars, but ordered by ASCII.
  private final static String ID_CHARS = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";

  // Pseudorandom number generator
  private final Random random;

  // Timestamp of last push, used to prevent local collisions if you push twice in one ms.
  private long lastTimestamp = 0L;

  // We generate 72-bits of randomness which get turned into 12 characters and appended to the
  // timestamp to prevent collisions with other clients.  We store the last characters we
  // generated because in the event of a collision, we'll use those same characters except
  // "incremented" by one.
  private int[] lastRandChars = new int[12];

  /**
   * Create a new IdGenerator with specific Random instance
   *
   * @param random Random, SecureRandom or ThreadLocalRandom
   */
  public IdGenerator(Random random) {
    this.random = random;
  }

  /**
   * Generate next chronological 20-character unique id
   *
   * @return New ID
   */
  public String nextId() {
    long now = System.currentTimeMillis();
    char[] result = new char[20];

    // random chars
    int[] randChars = compareAndUpdate(now);
    for (int i = 0; i < 12; i++) {
      result[i + 8] = ID_CHARS.charAt(randChars[i]);
    }

    // timestamp chars
    for (int i = 7; i >= 0; i--) {
      final Long module = now % 64;
      result[i] = ID_CHARS.charAt(module.intValue());
      now = (long) Math.floor(now / 64);
    }
    if (now != 0) {
      throw new AssertionError("We should have converted the entire timestamp.");
    }

    return new String(result);
  }

  /**
   * Compare the timestamp and update last record state. To be thread safe, all the compare and
   * update operation are included in this method
   *
   * @param timestamp Timestamp
   * @return Random Chars should be used
   */
  private synchronized int[] compareAndUpdate(long timestamp) {
    if (timestamp != this.lastTimestamp) {
      // If the timestamp has changed since last request, create a new random number.
      this.lastTimestamp = timestamp;
      for (int i = 0; i < 12; i++) {
        this.lastRandChars[i] = this.random.nextInt(64);
      }
    } else {
      // If the timestamp hasn't changed since last request, use the same random number, except incremented by 1.
      int i;
      for (i = 11; i >= 0 && lastRandChars[i] == 63; i--) {
        this.lastRandChars[i] = 0;
      }
      if (i < 0) {
        i = 11;
      }
      this.lastRandChars[i]++;
    }
    // return a new array
    return this.lastRandChars.clone();
  }
}

 

共有 人打赏支持
粉丝 6
博文 89
码字总数 50932
×
zhuqianli
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: