Skip to content

Instantly share code, notes, and snippets.

@dnene
Forked from dexterous/prime_generator.py
Created July 5, 2011 10:52
Show Gist options
  • Save dnene/1064647 to your computer and use it in GitHub Desktop.
Save dnene/1064647 to your computer and use it in GitHub Desktop.
prime number generator in python
def prime_generator():
yield 2
prev = [2]
potential = 3
while True:
while any(map(lambda x: potential % x == 0, prev)):
potential += 1
yield potential
prev.append(potential)
primes = prime_generator()
known_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
for n in known_primes:
assert primes.next() == n, n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment