programming-examples/python/Algorithms/Prime numbers sieve without fancy generators.py

20 lines
559 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
import itertools
def iter_primes():
# an iterator of all numbers between 2 and +infinity
numbers = itertools.count(2)
# generate primes forever
while True:
# get the first number from the iterator (always a prime)
prime = numbers.next()
yield prime
# this code iteratively builds up a chain of
# filters...slightly tricky, but ponder it a bit
numbers = itertools.ifilter(prime.__rmod__, numbers)
for p in iter_primes():
if p > 1000:
break
print p