public class solution14_4
{
public static void main (String [] args){
long start = System.currentTimeMillis();
int longest = 0;
int longestchain = 0;
for (int i=2; i<1000000; i++)
{
long n = i;
int count = 1;
while (n > 1)
{
/*if (n%2==0)
n/=2;
else
n=n*3+1;*/
n = NextNumber(n);
count++;
}
if (count>longestchain)
{
longestchain = count;
longest = i;
}
}
System.out.printf("\n%d: %d\n", longest, longestchain);
long stop = System.currentTimeMillis();
System.out.println("Run time = " + (stop - start) + " milliseconds");
}
public static long NextNumber (long value){
if ((value%2) == 0)
value /= 2;
else
value = value * 3 + 1;
if (value <0)//I set it here to check when it goes out of range
System.err.println("ERROR:Negative value!");
return value;
}
}