0

Possible Duplicate:
Project Euler, Problem 10 java solution not working

So, I'm attempting to solve Project Euler Problem 10 in Java, and I'm getting not the right answer. Here's my code:

public class Problem10 {
public static void main(String[] args)
{
    long sum =0;
    for(int i =3;i<2000000;i+=2)
    {
        if(isPrime(i))
        {
            sum+=i;
        }
    }
    System.out.println(sum);
}

public static boolean isPrime(int n)
{
    boolean prime = true;
    if (n<2) return false;
    if (n==2) return true;
    if (n%2==0) return false;
    for (int i = 3; i<=Math.sqrt(n);i+=2)
    {
        if (n%i==0)
        {
            prime=false;
            break;
        }
    }
    return prime;
}
}

Which prints out 142913828920, which project Euler tells me is wrong.

Any ideas?

(Also, I know my method for finding primes is terribly inefficient.)

Community
  • 1
  • 1
Colin DeClue
  • 2,194
  • 3
  • 26
  • 47
  • http://projecteuler.net/index.php?section=problems&id=10 – Isaac Aug 03 '10 at 18:19
  • this method is not *terribly* inefficient. In Haskell it is `2 : [n | n<-[3,5..], all ((> 0).rem n) [3,5..floor(sqrt(fromIntegral n))]]` - minimal trial division by odd numbers. The following *is* terribly inefficient, and *popular*: `sieve [2..] where sieve (x:xs) = x : sieve [n | n <- xs, rem n x > 0]` - excessive trial division by primes. Even slower and also quite popular is `[n | n<-[2..], all ((> 0).rem n) [2..n-1]]` - excessive trial division by all numbers. `[n | n<-[2..], not $ elem n [j*k | j<-[1..n-1], k<-[1..n-1]]]` is the champion - I saw it (in Java IIRC) on SO! – Will Ness Jun 16 '12 at 08:05

4 Answers4

2
for(int i =3;i<2000000;i+=2)

2 is prime.

dcp
  • 54,410
  • 22
  • 144
  • 164
1

You can accelerate your code a little bit by only dividing the prime numbers. For example, you can know that 35 is not a prime number by just trying to divide it by 2, 3, 5. No need to try 4. The trick is, any time you find a prime number, save it in a list or a vector. And in your isPrime function, just iterate the list until it hits sqrt(n) instead of every value in between 3..sqrt(n).

Leigh
  • 28,765
  • 10
  • 55
  • 103
dgg32
  • 1,409
  • 1
  • 13
  • 33
0

I found the following method very efficient , when i checked if a number is prime i only divide the number by the prime numbers previously found and are below square root of n . No need to check for all the numbers below square root of n . And it only takes more than one second !!

public class Problem10 {

    private static List<Long> listOfPrimes = new ArrayList<Long>();

    public static void main(String args[]) {

        long count = 0;
        for (long i = 2; i < 2000000; i++) {
            if (isPrime(i)) {
                count += i;
                System.out.print(i + " ");
            }
            if (i % 1000 == 0) {
                System.out.println();
            }
        }

        System.out.println("\nTotal " + count);

    }

    private static boolean isPrime(long n) {

        String strFromN = new Long(n).toString();
        if ((strFromN.length() != 1) && (strFromN.endsWith("2") || strFromN.endsWith("4") || strFromN.endsWith("5") || strFromN.endsWith("6") || strFromN.endsWith("8"))) {
            return false;
        }

        for (Long num : listOfPrimes) {
            if (num > Math.sqrt(n)) {
                break;
            }
            if (n % num.longValue() == 0) {
                return false;
            }
        }


        listOfPrimes.add(new Long(n));
        return true;
    }
}
Botz3000
  • 39,020
  • 8
  • 103
  • 127
web2dev
  • 557
  • 10
  • 28
  • no need to test even numbers above 2; it is already known as prime: replace `long count = 0; for (long i = 2; i < 2000000; i++) {` with `long count = 2; for (long i = 3; i < 2000000; i+=2) {`. So there's no need for string conversions now. – Will Ness Jun 16 '12 at 08:14
0

You can accelerate your code even more by using a Sieve.

Kizaru
  • 2,443
  • 3
  • 24
  • 39