O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

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

Start Free Trial

No credit card required

How to do it...

  1. Open src/Main.hs; we will add our prime number generator here.
  2. We will start with 2 as the initial prime number and continue with only odd numbers; this will remove all the factors of 2.
  3. We will assume that there are infinite prime numbers. We can write a list of primes as follows:
        primes :: [Integer]        primes= 2 : filterMultiples allMultiples [3,5..]        where          allMultiples = mergeMultiples $ map multiples primes          multiples i = map (i*) [i..]

Here, allMultiples are all multiples of all the primes; filterMultiples will weed out all those multiples from the list of odd numbers [3,5..]. All multiples are found out by lazily going over the primes that we are calculating and finding multiples of each.

  1. We need to implement filterMultiples ...

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

Start Free Trial

No credit card required