Sieve of Eratosthenes (Python)
The Sieve of Eratosthenes is an algorithm for rapidly locating all the prime numbers in a certain range of integers. It operates by marking as composite all nontrivial multiples of each prime in sequence until only primes remain unmarked. It is most well-suited to implementation using arrays, which are compact and feature random access.
<<streams.py>>= def streamfilter (pred, stream): for i in stream: if pred(i): yield i def ints(n): while True: yield n n = n+1 <<prime_sieve_streams.py>>= def primes: nums = ints(2) while True: prime = nums.next() yield prime def curfilter(v, p=prime): return ((v % p) != 0) nums = streamfilter (curfilter, ints)