天天看點

Problem 10 Summation of primes

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

Find the sum of all the primes below two million.

import math

def is_prime(n):
	if n < 2:return False
	for i in range(2,int(math.sqrt(n))+1,1):
		if not n % i:return False
	return True


print sum( [ i for i in range(10) if is_prime(i) ] )
print sum( [ i for i in range(2000000) if is_prime(i) ] )
           

繼續閱讀