Skip to content

Instantly share code, notes, and snippets.

@dmoney
Created October 9, 2017 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmoney/2a468a967eeaa5f25612d1adf3ce6fd1 to your computer and use it in GitHub Desktop.
Save dmoney/2a468a967eeaa5f25612d1adf3ce6fd1 to your computer and use it in GitHub Desktop.
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