Project Euler - Problem 10 - Summation of primes

coding algo projecteuler

Problem

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Solution

This is a simple sieve for primes.

n = int(2e6)
s = 0
p = [1]*n
for i in range(2, n):
    if p[i] and i>n**0.5 + 1:
        s += i
    elif p[i]:
        p[i*2::i] = [0]*(((n-1)//i) - 1)
        s += i

print s