街角_祝福

导航

project euler--10

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


posted on 2012-08-16 00:13  街角_祝福  阅读(106)  评论(0编辑  收藏  举报