Finding the largest simple number from 600851475143?

I am trying to solve problem 3 from http://projecteuler.net . However, when I run the program, nothing is printed. What am I doing wrong? Problem: what is the largest simple coefficient of the number 600851475143?

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);
    }
}
+3
source share
3 answers

The validation function primenot only returns false; even if it functions properly, your main loop is not looking for input coefficients at all, but rather just the largest prime, less than or equal to it. In pseudo code, your code is equivalent:

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

. , :

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

. , , , . , , , 1473 .

( ). , 1. , , . , , , , .

, : append

append(fs,d):
    return d

1, d , , d, , , .. d , .

+7

:

1) count 1 2. 1.

2) O (n ^ 2) N (, , № 1). .

+3

Project Euler , , , . , , .

, . , , , .

, 4 000 000 .

600851475143, , 20, . 600 , .

+2

Source: https://habr.com/ru/post/1629472/


All Articles