project Euler 14

来源:互联网 发布:金罗盘软件下载 编辑:程序博客网 时间:2024/06/06 20:05
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");    }}
0 0
原创粉丝点击