Implementing Eratosthenes Sieve
In this recipe, we will look at a prime number calculator called Eratosthenes Sieve. It is an old algorithm for finding prime numbers. The prime numbers are found by crossing out composite numbers. The sieve works as follows:
- Start with
2
, a known prime. Strike out all the numbers that are multiples of2
. - Start with the next unmarked number, which will be the next prime (since it is not divided by any prime before). Repeat the procedure of marking all the multiples.
- Repeat the procedure.
For more information, visit https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes.
Getting ready
Create a new project, eratosthenes
, using the simple
Stack template. Change into the project directory and build it:
stack new eratosthenes simple stack build
How to do it...
- Open
src/Main.hs
; we will add our prime number generator here. - We will start with
2
as the initial prime number and continue with only odd numbers; this will remove all the factors of2
. - We will assume that...