fork download
  1. from datetime import datetime
  2. d1=datetime.now()
  3. def eratosthenes():
  4. '''Yields the sequence of prime numbers via the Sieve of Eratosthenes.'''
  5. D = {} # map composite integers to primes witnessing their compositeness
  6. q = 2 # first integer to test for primality
  7. while 1:
  8. if q not in D:
  9. yield q # not marked composite, must be prime
  10. D[q*q] = [q] # first multiple of q not already marked
  11. else:
  12. for p in D[q]: # move each witness to its next multiple
  13. D.setdefault(p+q,[]).append(p)
  14. del D[q] # no longer need D[q], free memory
  15. q += 1
  16. suma=0
  17. for i in eratosthenes():
  18. if i>2000000:
  19. break
  20. suma+=i
  21. print(suma)
  22. print (datetime.now()-d1)
Success #stdin #stdout 4.66s 15880KB
stdin
Standard input is empty
stdout
142913828922
0:00:04.670477