何らかの理由で、これは常に値1を返します。これのポイントは、最も多くの回数(j = 1まで)ループする開始番号(1〜1,000,000)を見つけることです。jは最終的には常に1になります(コラッツ理論)。偶数の場合はjを2で除算し、奇数の場合は3で倍数して1を加算します。
#include <iostream>
using namespace std;
int collatz() {
int counter = 0;
int holder = 0;
for (int i = 999999; i > 1; i--){ // loops 999,999 times
for (int j = i; j != 1; counter++) { // loops until j = 1, records amount of loops
if (j % 2 == 0) { // if j is even, divide by 2, +1 to counter
j = j / 2;
} else {
j = (j*3) + 1; // if j is odd, multiply by 3 and add 1, +1 to counter
}
}
if (holder < counter){ // records highest number of loops
holder = counter;
counter = 0;
} else {
counter = 0;
}
}
return holder;
}
int main()
{
cout << collatz << endl;
return 0;
}