I am trying to calculate the sum of all the prime numbers below 2 million and since I had already written a function to find the prime numbers which are less than a given number, I simply wrote a new one that calls the older function and sum the items in that list.
But it seems like it's taking forever. How can I speed this code up?
def find_primes(n):
"Find the prime numbers below n"
primes=[];
for i in range(2,n):
for fac in range (2,i):
if i!=fac and i%fac == 0:
break
else:
primes.append(i)
return primes
def add_primes(m):
"Sum all the prime numbers below m"
newlist=find_primes(m);
t=sum(newlist);
return t
PS: I'm kind of a novice about Python, so I will be glad if you can nicely explain my mistakes. Thanks in advance.