The Jupyter Notebook version is in Python 3. http://nbviewer.jupyter.org/github/4dsolutions/Python5/blob/master/Sieve%20of%20Eratosthenes.ipynb Note that the actual output is behind the console button (P).

from math import sqrt as root2, floor sprite = codesters.Sprite("Eratosthenes_7f2") stage.set_background("space") def eratosthenes(n): the_max = floor(root2(n)) sieve = list(range(0, n+1)) eliminator = 2 while True: if eliminator > the_max: break print "Eliminating multiples of:", eliminator for k in range(2 * eliminator, n + 1, eliminator): sieve[k] = 0 while sieve[eliminator + 1] == 0: eliminator += 1 else: eliminator = sieve[eliminator + 1] # shrink me down (compact the sieve) sieve = [n for n in sieve if n != 0][1:] # list comprehension! return sieve results = eratosthenes(1000) for row in range(0, len(results), 10): print(", ".join(map(str, results[row:row+10])))
  • Run Code
  • Show Console
  • Codesters How To (opens in a new tab)