PROBLEM:
Start with an integer n. If n is even, divide by 2. If n is odd, multiply by 3 and add 1. Repeat this process with the new value of n, terminating when n = 1. Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.
For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.
The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.
For each pair of input integers i and j, output i, j in the same order in which they appeared in the input and then the maximum cycle length for integers between and including i and j. These three numbers should be separated by one space, with all three numbers on one line and with one line of output for each line of input.
SAMPLE INPUT:
1 10
100 200
201 210
900 1000
SAMPLE OUTPUT:
1 10 20
100 200 125
201 210 89
900 1000 174
MY CODE:
Code:
#include"stdafx.h"
#include<iostream>
using namespace std;
long long cycleLength(const int& n)
{
int num = n;
long long length = 0;
while (num != 1)
{
++length;
if (num % 2 == 0)
num /= 2;
else
num = num * 3 + 1;
}
++length;
return length;
}
int main()
{
int i, j;
while (cin >> i >> j)
{
long long maxLength = 0;
long long temp;
for (int itr = i; itr <= j; ++itr)
{
temp = cycleLength(itr);
if (maxLength < temp)
maxLength = temp;
}
cout << i << ' ' << j << ' ' << maxLength << endl;
}
}