Skip to content

Instantly share code, notes, and snippets.

@andlima
Created October 1, 2011 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andlima/1256380 to your computer and use it in GitHub Desktop.
Save andlima/1256380 to your computer and use it in GitHub Desktop.
A simple generator of the primes sequence
primes_list = [2, 3]
def primes(maximum):
for p in primes_list:
if p > maximum:
return
yield p
for m in range(primes_list[-1] + 2, maximum + 1, 2):
sqrt_m = int(m ** 0.5)
if all(m % p != 0 for p in primes(sqrt_m)):
primes_list.append(m)
yield m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment