project euler10

Question:

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

Find the sum of all the primes below two million.


Code:

import math
def IsPrime(n):
    if n==1:
        return False
    else:
        i = 2
        while n%i!=0 and i<=math.sqrt(n):
            i += 1
        if i<=math.sqrt(n):
            return False
        else:
            return True

print(sum([i for i in range(2,2000000) if IsPrime(i)]))

answer:

142913828922


原文地址:https://www.cnblogs.com/arbboter/p/4225235.html