The preceding algorithm is inspired by Richard Bird's list-based algorithm for the sieve. The algorithm carefully uses laziness, recursion, and infinite lists to create an infinite list of prime numbers. The following diagram will help you visualize the algorithm:
In the implementation, we carry out following steps:
- We will start with a prime 2. Then we construct an infinite list of primes 2-- filterMultiples allMultiples [3,5..]. The filterMultiples function remove all the multiples of primes from the infinite list of odd numbers.
- The allMultiples function is interesting; it lazily creates a list of multiples for each prime ...