-
-
Save benjamintanweihao/1375509 to your computer and use it in GitHub Desktop.
[fork] Sieve
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
"""Translated from Haskell: | |
let sieve(p:xs) = p : sieve (filter (\ x -> x `mod` p /= 0) xs) in sieve [2..] | |
""" | |
from itertools import ifilter | |
def ints(k): | |
while True: | |
yield k | |
k+=1 | |
# needed due to scoping issues | |
def make_filter_pred(p): | |
return lambda x: x % p != 0 | |
def sieve(g): | |
while True: | |
p = g.next() | |
yield p | |
g = ifilter(make_filter_pred(p), g) | |
primes = sieve(ints(2)) | |
for i in xrange(10000): | |
print next(primes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment