Skip to content

Instantly share code, notes, and snippets.

@rctay
Created March 15, 2018 10:06
Show Gist options
  • Save rctay/9bbbe11ee203205cf6d69f28155e976c to your computer and use it in GitHub Desktop.
Save rctay/9bbbe11ee203205cf6d69f28155e976c to your computer and use it in GitHub Desktop.
def allnumbers(i=1):
while True:
yield i
i += 1
def fastforward(it, till, it_head=None):
n = next(it) if it_head is None else it_head
while n < till:
n = next(it)
return n
def multiples(n):
for m in allnumbers():
yield n * m
def exclude(xs, ys):
y = None
for x in xs:
y = fastforward(ys, x, it_head=y)
if x is not y:
yield x
def sieve(s=allnumbers(i=2)):
while True:
s1 = next(s)
yield s1
s = exclude(s, multiples(s1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment