1000 are Prime !

What is the sum of first 1000 prime numbers ?

3682933 3682943 3682923 3682913

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.

4 solutions

Aryan Gaikwad
Feb 24, 2015

Java solution (try online - https://ideone.com/uTCwG4) -

static int n = 1, sum = 2;
public static void main (String[] args) throws java.lang.Exception
{
    for(int i=2;;i++)
        if(isPrime(i)){
            sum += i;
            if(++n == 1000){
            System.out.println(sum);
            return;
            }
        }
}

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;
}
Fox To-ong
Jan 29, 2015

since all the items were prime numbers up to 1000, the sum should be a prime number

Rafael Nunes
Jan 3, 2015

I wrote a C++ algorithm to it, just look at: https://gist.github.com/rafaelcn/e5391bce91bcde61c135

Brock Brown
Jan 2, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from math import sqrt
def is_prime(x):
    if x < 2:
        return False
    i = 2
    while i <= sqrt(x):
        if x % i == 0:
            return False
        i+=1
    return True
primes = set()
i = 2
while len(primes) < 1000:
    if is_prime(i):
        primes.add(i)
    i += 1
print "Answer:", sum(primes)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...