Created
October 9, 2017 03:55
-
-
Save dmoney/2a468a967eeaa5f25612d1adf3ce6fd1 to your computer and use it in GitHub Desktop.
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
from itertools import islice | |
def ints_from(start=2, step=1): | |
next = start | |
while True: | |
next += step | |
yield next | |
def take(n, iterable): | |
"""Return first n items of the iterable as a list | |
From Itertools Recipes: | |
https://docs.python.org/3/library/itertools.html#itertools-recipes | |
""" | |
return list(islice(iterable, n)) | |
def primes(): | |
"Compute primes using the Sieve Of Eratosthenes method" | |
primes_found = set() | |
for num in ints_from(start=2): | |
if not any(num % known_prime == 0 for known_prime in primes_found): | |
primes_found.add(num) | |
yield num | |
if __name__ == '__main__': | |
print(take(100, primes())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment