我在計算產生最長的Collatz序列的數字。但這是一個奇怪的問題。當n
是3時,3n+1
變爲38654705674
。我沒有看到錯誤。這裏是全碼:運算符*和+在數字火星中產生錯誤的結果
/* 6.c -- calculates Longest Collatz sequence */
#include <stdio.h>
long long get_collatz_length(long long);
int main(void)
{
long long i;
long long current, current_count, count;
current_count = 1;
current = 1;
for(i=2;i<1000000;i++)
{
// works fine when i is 2 the next line take eternity when i is 3;
count = get_collatz_length(i);
if(current_count <= count)
{
current = i;
current_count = count;
}
}
printf("%lld %lld\n", current, current_count);
return 0;
}
long long get_collatz_length(long long num)
{
long long count;
count = 1;
while(num != 1)
{
printf("%lld\n", num);
if(num%2)
{
num = num*3+1; // here it is;
}
else
{
num/=2;
}
count++;
}
puts("");
return count;
}
由於此行爲'count = get_collatz_length(i); 3',所以不會編譯。而'printf(「%lld%lld/n」,current,current_count);'不會打印出新行 – 2014-12-06 10:33:26
啊!,它是上一行註釋的一部分。由於垂直滾動條,我只是將它移動到一行中。它不是代碼的一部分。謝謝你指出。 – silentboy 2014-12-06 10:35:42
什麼是你的平臺上的'sizeof(long long)'? – slugonamission 2014-12-06 10:35:51