作为作业的附加问题,我们被要求找到产生最长 collatz 序列的 10 个起始数字 (n)。(其中 0 < n < 10,000,000,000)我编写的代码有望实现这一点,但我估计需要整整 11 个小时才能计算出答案。
我注意到了一些小的优化,比如从最大到最小,所以添加到数组中的操作更少,并且只计算 10,000,000,000/2^10 (=9765625) 和 10,000,000,000 之间,因为必须有 10 个长度更长的序列,但是我看不出还有什么我能做的。任何人都可以帮忙吗?
相关代码序列搜索算法
long[][] longest = new long[2][10]; //terms/starting number
long max = 10000000000l; //10 billion
for(long i = max; i >= 9765625; i--) {
long n = i;
long count = 1; //terms in the sequence
while(n > 1) {
if((n & 1) == 0) n /= 2; //checks if the last bit is a 0
else {
n = (3*n + 1)/2;
count++;
}
count++;
}
if(count > longest[0][9]) {
longest = addToArray(count, i, longest);
currentBest(longest); //prints the currently stored top 10
}
}
存储算法
public static long[][] addToArray(long count, long i, long[][] longest) {
int pos = 0;
while(count < longest[0][pos]) {
pos++;
}
long TEMP = count; //terms
long TEMPb = i; //starting number
for(int a = pos; a < longest[0].length; a++) {
long TEMP2 = longest[0][a];
longest[0][a] = TEMP;
TEMP = TEMP2;
long TEMP2b = longest[1][a];
longest[1][a] = TEMPb;
TEMPb = TEMP2b;
}
return longest;
}