The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Another "prime number" question. Let's reuse and adapt our solution from problem 7:
def f1(n):
# Define test for primeness
def isprime(x):
if x <= 1: return False
c = 2
while c <= x**0.5:
if x%c == 0:
return False
if c != 2:
c += 2
else:
c += 1
return True
# Iterate over odd numbers and test for primeness
res = 2
for i in xrange(3,int(n),2):
if isprime(i):
res += i
# Return the sum
return resThis is starting to feel slow, though. On my machine, the correct result is returned in about 47 seconds. I'm gonna have to either better optimize my prime generation algorithm or find a new one altogether.
