Project Euler #3 takes forever in Java

后端 未结 9 792
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 03:01

Problem #3 on Project Euler is:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 60085147514

9条回答
  •  执笔经年
    2020-12-04 03:23

    This one works perfectly!!

    public class Puzzle3 {
    public static void main(String ar[])
    {
        Long i=new Long("1");
        Long p=new Long("600851475143");
        Long f=new Long("1");
        while(p>=i)
        {
            if(p%i==0)
            {
                f=i;
                p=p/i;
                int x=1;
                i=(long)x;
            }
            i=i+2;
        }
        System.out.println(f);
    }
    

    }

提交回复
热议问题