Wednesday, 4 December 2013

Project Euler Problem 10 - Solved

Q) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

Solution:

size = 2000000
num = [True] * (size+1)

def Prime():
    for i in xrange(2,((size/2))):
        if num[i]:
            for j in xrange(i*2,size+1,i):
                num[j] = False
    ans = sum((i for i in xrange(size+1) if num[i]))               
    return ans-1

print Prime()




No comments:

Post a Comment