With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

How it works...

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required