Skip to content

Instantly share code, notes, and snippets.

@rctay

rctay/sieve.py

Created Mar 15, 2018
Embed
What would you like to do?
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