我正在尝试用 c++ 完成 Project Euler Problem 14,但我真的被卡住了。现在,当我运行问题时,它卡在 So Far:计数最高的数字:113370,计数为 155 So Far:计数最高的数字,但是当我尝试将 i 值更改为超过 113371 时,它可以工作。到底是怎么回事??
问题是:
为正整数集定义了以下迭代序列: n → n/2(n 为偶数) n → 3n + 1(n 为奇数)
使用上面的规则并从 13 开始,我们生成以下序列:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 可以看出,这个序列(从 13 开始,到 1 结束)包含 10 个项。虽然尚未证明(科拉茨问题),但认为所有起始数字都以 1 结束。100 万以下的哪个起始数字产生最长的链?
#include<stdio.h>
int main() {
int limit = 1000000;
int highNum, number, i;
int highCount = 0;
int count = 0;
for( number = 13; number <= 1000000; number++ )
{
i = number;
while( i != 1 ) {
if (( i % 2 ) != 0 ) {
i = ( i * 3 ) + 1;
count++;
}
else {
count++;
i /= 2;
}
}
count++;
printf( "So Far: the number with the highest count: %d with the count of %d\n",
number, count );
if( highCount < count ) {
highCount = count;
highNum = number;
}
count = 0;
//break;
}
printf( "The number with the highest count: %d with the count of %d\n",
highNum, highCount );
}