Skip to content

Instantly share code, notes, and snippets.

@benjamintanweihao
Forked from rctay/sieve1.py
Created November 18, 2011 03:28
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 benjamintanweihao/1375509 to your computer and use it in GitHub Desktop.
Save benjamintanweihao/1375509 to your computer and use it in GitHub Desktop.
[fork] Sieve
"""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