[Project Euler]Problem 10

Problem:
  The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
  Find the sum of all the primes below two million.

问题:
  小于10的所有素数的和为 17。求小与2,000,000的所有素数的和。 

方法:
  筛法求素数。 

View Code
def primes(n):
if n<2:
raise StopIteration
else:
yield 2
l
=[True for i in range((n-1)//2)]
for i in range(len(l)):
if l[i]:
yield 2*i+3
for j in range(3*i+3,len(l),2*i+3):
l[j]
=False
def f(n):
return sum(primes(n))
print(f(2000000))

作者: class   发布时间: 2011-06-07