project Euler 14

417ms
import java.util.Date;

/**
* Created by Administrator on 2015/5/18.
*/
 class E14 {
  public static long[] compute(long number)
  {
      long[] result=new long[2];
      result[0]=number;
      int count=0;
      while (number!=1)
      {
          if(number%2==0)
          {
              number=number/2;
              count++;
          }
          else
          {
              number=3*number+1;
              count++;
          }
      }
      result[1]=count;
      return result;
  }

    public static void main(String[] args) {
        long startTime=System.currentTimeMillis();
        long maxResult[]=compute(1);
        for(int i=2;i<1000000;i++)
        {
            long tempresult[]=compute(i);
            if(maxResult[1]<tempresult[1])
            {
                maxResult[0]=tempresult[0];
                maxResult[1]=tempresult[1];
            }
        }
        long endTime=System.currentTimeMillis();
        System.out.println(maxResult[0]+"  "+maxResult[1]);
        System.out.println(endTime-startTime+" ms");
    }


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章