-
-
Save dnene/1064647 to your computer and use it in GitHub Desktop.
prime number generator in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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