1

Here is my code:

public static boolean isPrime(long num)
    {
        for(long i=2; i<=num/2; i++)
        {
            if(num%i==0)
            {
                return false;
            }
        }
        return true;
    }

    public static long findLargestPrimeFactor(long n)
    {
        long max=0;
        for(long factor=2; factor<n; factor++)
        {
            if(n % factor==0)
            {
                if(isPrime(n/factor)==true)
                {
                    max=factor;
                }
            }
        }
        return max;
    }

However, when I run it, it says that the integer 600851475143 is too large. can anyone help me?

sashkello
  • 17,306
  • 24
  • 81
  • 109

1 Answers1

7

I'm going to wager you're calling

 isLargestPrimeFactor(600851475143);

since that's the problem given in Project Euler #3. This will fail to compile with an error:

  error: integer number too large: 600851475143

Correct? If that's the case, try:

 isLargestPrimeFactor(600851475143L);

The "L" suffix tells the compiler that you mean it to be a long literal, not an int (otherwise the compiler will treat it as an integer literal, and it's too large to fit in an int)

James
  • 8,512
  • 1
  • 26
  • 28