Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.math.BigInteger;
  2. import java.util.*;//vector
  3. public class solution14_2
  4. {
  5.     // instance variables - replace the example below with your own
  6.     public static Vector table = new Vector();
  7.     public static BigInteger zero = new BigInteger("0");
  8.     public static BigInteger one = new BigInteger("1");
  9.     public static BigInteger two = new BigInteger("2");
  10.     public static BigInteger three = new BigInteger("3");
  11.     public static BigInteger million = new BigInteger("1000000");
  12.    
  13.     // public static int table;
  14.    
  15.     public static void main (String [] args){
  16.         /*int i = 0;
  17.         int chainlength = 0;
  18.         int longest = 0;
  19.         int longestchain = 0;
  20.         int value = 0
  21.         */
  22.         BigInteger chainlength = zero;
  23.         BigInteger longest = zero;
  24.         BigInteger longestchain = zero;
  25.         BigInteger i = zero;
  26.         BigInteger value = zero;
  27.         table.setSize(1000000);
  28.         while (i.compareTo(million)<0){
  29.             table.set(i.intValue(),0);
  30.             i = i.add(one);
  31.         }
  32.        
  33.         for (i=one; i.compareTo(million)<0; i = i.add(one))
  34.         {
  35.             //System.out.println(i);
  36.             chainlength = zero;
  37.             value = i;
  38.             while (!value.equals(one)){
  39.                 chainlength=chainlength.add(one);
  40.                 value = NextNumber(value);
  41.                 if (value.compareTo(million)>0)
  42.                     continue;
  43.                 else if (value.equals(million))
  44.                     continue;
  45.                 else if (! ((table.get(value.intValue())).equals(zero))){
  46.                     //chainlength += table.get(value);
  47.                     //                            (BigInteger)table.get(value.intValue())
  48.                     int a = value.intValue();
  49.                     int b = (Integer) table.get(a);
  50.                     chainlength = chainlength.add(BigInteger.valueOf(B));
  51.                     break;
  52.                 }
  53.             }
  54.             table.set(i.intValue(),chainlength.intValue());
  55.             //            table.set(i,chainlength);
  56.             //System.out.println(i+"->"+chainlength);
  57.             if (longestchain.compareTo(chainlength)<0){
  58.                 longest = i;
  59.                 longestchain = chainlength;
  60.             }
  61.         }
  62.         System.out.printf("\n%d: %d\n", longest, longestchain);
  63.     }
  64.    
  65.     public static BigInteger NextNumber (BigInteger value){
  66.         //System.out.println("Value: " + value);
  67.         if ((value.remainder(two)).compareTo(zero)==0)
  68.         {
  69.             value = value.divide(two);
  70.             // System.out.println("New Value: " + value);
  71.         }
  72.         else
  73.         {
  74.             value = value.multiply(three).add(one);
  75.             // System.out.println("New Value: " + value+"\n***********");
  76.         }
  77.         if (value.compareTo(zero)<0)
  78.             System.err.println("ERROR:Negative value!");
  79.         return value;
  80.     }
  81. }