The 3n + 1 problem

 avatar
user_6817964
c_cpp
2 years ago
640 B
1
Indexable
Never
#include <iostream>

using namespace std;

main(){
    int i, j;
    while(cin >> i >> j){
        cout << i << " " << j << " ";

        if(i > j){
            int temp = i;
            i = j;
            j = temp;
        }

        int max = 0;

        for(int x = i; x <= j; x++){

            int sum = 1, k = x;

            while(k != 1){

                sum++;

                if(k % 2 == 1)
                    k = k * 3 + 1;
                else
                    k /=2;
            }

            if(sum > max)
                max = sum;
        }
        cout << max << endl;

    }

}