Project Euler #3

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2012-11-06T04:57:52Z Indexed on 2012/11/06 4:59 UTC
Read the original article Hit count: 139

Filed under:
|

Question: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143?

I found this one pretty easy, but running the file took an extremely long time, it's been going on for a while and the highest number I've got to is 716151937.

Here is my code, am I just going to have a wait or is there an error in my code?

        //User made class
public class Three
{   
        public static boolean checkPrime(long p)
        {
            long i;
            boolean prime = false;
        for(i = 2;i<p/2;i++)
        {
            if(p%i==0)
            {
                prime = true;
                break;
            }
        }
    return prime;
    }   

}

    //Note: This is a separate file
public class ThreeMain
{
    public static void main(String[] args)
        {
            long comp = 600851475143L;
            boolean prime;
            long i;
            for(i=2;i<comp/2;i++)
            {
                if(comp%i==0)
                {
                    prime = Three.checkPrime(i);
                    if(prime==true)
                    {
                        System.out.println(i);
                    }
                }
            }
        }       
}

© Stack Overflow or respective owner

Related posts about java

Related posts about project-euler