Hi, I was working on a solution to problem ten of Project Euler, which states:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
I've got code that solves this using eratosthenes sieve, but it loops painful though my total list:
def is_prime(Number):
if Number == 1: return False
elif Number == 2: return True
elif not Number % 2: return False
elif Number < 9 : return True
else:
n = 3
while n < Number:
if not Number % n:
return False
else:
n += 2
return True
def sum_primes(max_num):
total = range(3, max_num+1, 2)
for n in total:
if is_prime(n):
m = n+n
t = 0
while m < max(total):
t += 1
try:
total.remove(m)
m += n
except ValueError:
m += n
if t == 1000:
print 'Looping though ' + str(n) + '; now at ' + str(m)
t = 0
else:
total.remove(n)
return sum([2] + total)
print sum_primes(2000000)
I'm not sure what is slowing this down so much. Thanks in advence!