This is a problem for an online judge. It takes two inputs, i and j where the largest 3n+1 cycle length has to be found between the two inputs.
The program should terminate when x becomes 1. But when it is 1, the code does not terminate but continues to loop 1. Thanks in advance for any help.
#include <iostream>
using namespace std;
int main(){
int i, j, temp_i, temp_j, counter, max;
max = 0;
cin >> i >> j;
temp_i = i;
temp_j = j;
for(int x = i; x < j; x++){
counter = 1;
while(x != 1){
if(x % 2 == 0){
x = x/2;
// cout << x << endl;
}
else{
x = 3*x + 1;
}
counter++;
}
if(counter > max){
max = counter;
}
}
cout << temp_i << " " << temp_j << " " << max << endl;
return 0;
}
xbecomes 1 at any time? It looks like it can possibly diverges to infinity.