Sum of Primes upto 2 Million

Find the sum of all the primes below two million.

  • Two Million = 2,000,000

Try other problems here .


The answer is 142913828922.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class main {
    static boolean isPrime(int n) {
        if (n%2==0) return false;

        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    }

    public static void main( String[] args) {
        long sum=2;
        for (int j=3;j<2_000_000;j=j+2){
            if(isPrime(j))
                sum+=j;
        }
        System.out.println("SUM : "+sum);
    }
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...