## 小算法记录 原

zhuqianli

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;
i--; // i--使得i的值不变,即能把含有的所有的当前质因数， 如 2，除尽后， 再去除下一个指数， 如 3
}

return result;
}

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

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();
}
}``````

### zhuqianli

Go Ethereum 项目是美图技术团队在以太坊上实现的 DPoS 算法，是以太坊协议的 Go 语言实现，目前尚处于实验阶段。 查看官方的实现版本>>>https://www.oschina.net/p/go-ethereum 基于 Ethere...

06/15
0
0

Jack_lin
2017/03/18
0
0
MySQL的JOIN（二）：JOIN原理

04/01
0
0

2017/12/05
0
0
k-means算法 python实现

k-means算法 算法描述 需要事先指定簇的个数k，算法开始随机选择k个记录点作为中心点，然后遍历整个数据集的各条记录，将每条记录归到离它最近的中心点所在的簇中，之后以各个簇的记录的均值...

zxszxs
05/28
0
0

Function函数式接口

Function函数式接口传入一个参数，返回一个值。 然后我们使用这个写个demo看看： 输出： 接口内部还有两个default方法和一个static方法，然后我们先看一下static方法 返回一个始终返回其输入...

woshixin
5分钟前
0
0

1、开发者和架构师之间最大的区别是什么？ 架构师和开发者一样，也经常写代码，简单的说，开发者和架构师之间最大的区别就是技术领导力。 软件架构师的角色需要理解最重要的架构驱动力是什么...

James-
35分钟前
1
0
java框架学习日志-4

38分钟前
2
0
20181213 上课截图

54分钟前
1
0