Largest prime factor program takes aaaages - Java

后端 未结 6 1508
一向
一向 2020-12-18 12:28

So this is problem 3 from project Euler. For those who don\'t know, I have to find out the largest prime factor of 600851475143. I have the below code:

impor         


        
6条回答
  •  旧时难觅i
    2020-12-18 12:38

    change

    for(long i = 2; i <= limit; i++)

    to

    // add the one for rounding errors in the sqrt function
    new_limit = sqrt(limit) + 1;
    // all even numbers are not prime 
    for(long i = 3; i <= new_limit; i+=2)
    {
    ...
    }
    

    Factoring 1,000,000 for example instead of iterating 1,000,000 times the thing only needs to do around 500 iterations.

提交回复
热议问题