Skip to content

Instantly share code, notes, and snippets.

@dexterous
Created July 5, 2011 09:32
Show Gist options
  • Save dexterous/1064555 to your computer and use it in GitHub Desktop.
Save dexterous/1064555 to your computer and use it in GitHub Desktop.
prime number generator in python
def prime_generator():
potential = 2
primes = []
while True:
while any(potential % x == 0 for x in primes):
potential += 1
yield potential
primes.append(potential)
prime_numbers = prime_generator()
known_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
for n in known_primes:
assert prime_numbers.next() == n, n
@dexterous
Copy link
Author

SHA: 4583371527fa16f34bb57d639b7e2a56b47cad20
refactored as suggested by @dnene in https://gist.github.com/1064649

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment