I am trying to solve the 10 problem in the Project Euler. It consists on finding the sum of all the primes below two million. I wrote the following code based on the Sieve of Eratosthenes.
import time
t0 = time.time()
n=200000
liste=list(range(2,n))
k=2
s=2
while k <=n:
liste=list(set(liste)-set(range(k,n,k)))
if liste!=[]:
k=min(liste)
s+=k
else:
break
print(s)
t1 = time.time()
total = t1-t0
print(total)
I tested the above code for n=200000, but it is too slow for n=2000000. I would be very thankful to get any help to improve this program.
Always try to measure empirical complexity of your code at several ranges.
Your code is slow because of how you find the set difference, and that you always convert between set and list and back. You should be using one set throughout, and updating it in place with
To find the min element, you can just call
min(sete)
on the set, no list conversion required.Because of the inefficient search for the min element the overall complexity of the resulting code will be nearing n^1.5, which is not too bright, but not too terrible either. In particular, it finishes in under 4.9 seconds on ideone.com, finding the sum for the primes below 2000000, and 0.5 seconds for 400000 (with the additional optimization of working with odds only, in the first place).