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

Find the sum of all the primes below two million.


In [3]:
def primes_below(n):
    from math import sqrt
    from bisect import bisect_right
    primes = [2]
    i = 3

    while i < n:
        ok = True
        for p in primes[:bisect_right(primes, sqrt(i))]:
            if i % p == 0:
                ok = False
                break
        if ok:
            primes.append(i)
            yield i 
        i += 1
    return primes

In [9]:
sum(primes_below(2*10**6))


Out[9]:
142913828922

In [10]:
%timeit sum(primes_below(2*10**6))


1 loops, best of 3: 8.22 s per loop