Problem 10

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

Find the sum of all the primes below two million.

Solution 1

Let’s look at the answer:

n = 3
primes = []
total = 2

# no multiples of 2
while n < 2000000:
    isPrime = True
    sqrt_n = n**0.5

    for p in primes:

        if p > sqrt_n:
            break

        if not n%p:
            isPrime = False
            break

    if isPrime:
        primes += [n]
        total += n

    n += 2


print(total)

The output: 142913828922

This run: ~5.3197691440582275