Sum of Primes Less than 10,008

Computer Science Level pending

Write a program to add up all of the prime numbers less than or equal to 10,008.

You may want to have a look at the Sieve of Eratosthenes .


The answer is 5746403.

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

Peter Chiappini Staff
Jul 28, 2016
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def sumPrimes(n):
    sum = 0
    sieve = [True] * (n+1) #initialize sieve
    for p in range(2, n):
        if sieve[p]:
            sum += p #the next number marked true will be prime
            for i in range(p*p, n, p):
                sieve[i] = False #mark all multiple of p as not prime starting with p*p
    return sum
print sumPrimes(10008)

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...