Java – find the largest prime number in 600851475143?

I tried from http://projecteuler.net Problem solving 3 But when I run the program, it doesn't print out

public class project_3 
{
    public boolean prime(long x)   // if x is prime return true
    {
        boolean bool = false;

        for(long count=1L; count<x; count++)
        {
            if( x%count==0 )
            {
                bool = false;
                break;
            }
            else { bool = true; }
        }
        return bool;
    }

    public static void main(String[] args)
    {
        long ultprime = 0L;  // largest prime value
        project_3 object = new project_3();

        for(long x=1L; x <= 600851475143L; x++)
        {
            if( object.prime(x)==true )
            {
                ultprime = ((x>ultprime) ? x : ultprime);
            }
        }
        System.out.println(ultprime);
    }
}

Solution

Your main checking function not only always returns errors; Even if it works properly, your main loop will not look for the factor of the input number at all, but only for the maximum prime number less than or equal to it In pseudo code, your code is equivalent to:

foo(n):
    x := 0 ;
    foreach d from 1 to n step 1:
        if is_prime(d):          // always false
            x := d
    return x                     // always 0

is_prime(d):
    not( d % 1 == 0 )            // always false

But you don't need prime checking at all Find all the factors of the following numbers to the trial division:

factors(n):
    fs := []
    d  := 2
    while ( d <= n/d ):
        if ( n % d == 0 ): { n := n/d ; fs := append(fs,d) }
        else:              { d := d+1 }
    if ( n > 1 ): { fs := append(fs,n) }
    return fs

The separability test is performed only at the square root of the number As found, each factor is decomposed into the decomposed quantity, which further reduces the running time The number of factorizations involved runs immediately, requiring only 1473 iterations

Through construction, all the factors found are guaranteed to be prime numbers (which is why prime checking is not necessary) To achieve this, it is important to calculate the possible divisor in ascending order. 1 Ascending order is also most effective because any given number is more likely to have a larger prime number and a smaller prime factor Enumerating primes instead of odds is not necessary, but it will be more efficient if you have an effective way to obtain these primes

Adding the above to find the biggest factor is trivial: just add as

append(fs,d):
    return d

Because when any compound divisor D of the original number is decomposed, when we reach D, we have divided its prime factor by the original number, the reduced number will have no common prime factor with it, that is, even if it is in addition to the original number, D will not separate the reduced number

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>