Created
August 6, 2017 06:11
-
-
Save nieldomingo/52a2591bb66a0f02fa5ecc62251226bf to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes using Python Generators
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 sieve(): | |
def _sieve(): | |
current = 2 | |
while True: | |
yield current | |
current += 1 | |
def _new_gen(s, current_prime): | |
return (n for n in s if n % current_prime != 0) | |
s = _sieve() | |
while True: | |
current_prime = next(s) | |
yield current_prime | |
s = _new_gen(s, current_prime) | |
s = sieve() | |
for _ in range(30): | |
print(next(s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment