Created
March 15, 2018 10:06
-
-
Save rctay/9bbbe11ee203205cf6d69f28155e976c 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
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