Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class solution14_4
  2. {  
  3.     public static void main (String [] args){
  4.         long start = System.currentTimeMillis();
  5.         int longest = 0;
  6.         int longestchain = 0;
  7.        
  8.         for (int i=2; i<1000000; i++)
  9.         {
  10.             long n = i;
  11.             int count = 1;
  12.             while (n > 1)
  13.             {
  14.                 /*if (n%2==0)
  15.                     n/=2;
  16.                 else
  17.                     n=n*3+1;*/
  18.                 n = NextNumber(n);
  19.                 count++;
  20.             }
  21.             if (count>longestchain)
  22.             {
  23.                 longestchain = count;
  24.                 longest = i;
  25.             }
  26.         }
  27.         System.out.printf("\n%d: %d\n", longest, longestchain);
  28.         long stop = System.currentTimeMillis();
  29.         System.out.println("Run time = " + (stop - start) + " milliseconds");
  30.     }
  31.    
  32.     public static long NextNumber (long value){
  33.         if ((value%2) == 0)
  34.             value /= 2;
  35.         else
  36.             value = value * 3 + 1;
  37.         if (value <0)//I set it here to check when it goes out of range
  38.             System.err.println("ERROR:Negative value!");
  39.         return value;
  40.     }
  41. }