# Functional Prime Number Sieve

06 Jun 2017In a post a few years ago I discussed primality testing. For some reason I thought I described the basic prime number sieve (I’m pretty sure there is only one) there, but apparently I didn’t. No worries. The prime number sieve solves a different problem; that of generating a list of the first $N$ primes. One could use a primality testing method to do this as well; namely filter a list of numbers with the primality test as a predicate; but sieve based methods are faster and conceptually much easier.

The prime number sieve iteratively filters out all the composite numbers, with the caveat that the leading number in the input is a prime. For , we put aside $2$, the smallest number, and filter out all multiples of $2$; then put aside $3$ and filter out multiples of $3$, and so on. This process, shown below as a decreasing sequence, terminates with a set of primes.

At each stage we have a list of some primes and a residual set, which if it is nonempty must be filtered; this process terminates when the residual set is empty. Filtering and subsequent residuals sort of defines the set of all primes; however it is an infinite process.

When $n$ primes have been filtered the state looks like . Here we denote the residual set $A_{p_i}$ where $p_i$ is the leading/smallest number of the residual set. Consecutive residuals are related by

Anyone schooled in imperative programming ( which I guess is most of the world ;)) would go about implementing a function which returns the first $N$ primes by iteratively filtering and collecting primes in a loop. I should emphasize that when I write **iteratively**, I mean that the instructions for filtering and collecting primes are done *eagerly*.

Putting aside the computation for the first $N$ primes, all primes ( which is an infinite set ) can be expressed as a mathematical set, without really specifying how to compute the next prime.

Okay, the second set from above is practically a tautology…but I digress. The point here is that in mathematical notation we are free to declare an expression for the set all primes, even if there is no set notation for it. Generally in mathematics it’s common to express and manipulate infinite sequences and series. Being somewhat aware of Haskell’s declarative nature, I wondered at the outset if I could create an expression for all primes and subsequently generate finite lists of primes as needed. As you might guess the answer is Yes :)

For the uninitiated, referencing Haskell may seem like a non sequitur. Haskell codes are composed of expressions, which are meant to be true statements, as opposed to instructions for the computer to execute in sequence. Expressions in a Haskell program ( or any pure functional language for that matter) are executed as needed, **lazily**. As opposed to **eagerly**. Computing the set of all primes via the iterative/imperative prime number sieve implementation would be an infinite loop. Which would never end… And that’s the key difference: declaring/expressing the set of all primes versus computing all primes.

We can easily adapt the sieve described earlier. Haskell is nothing if not functional, so we define the following function,

I first tried to explicitly iterate through a finite list of consecutive numbers; as I was developing I was trying to get a feel for haskell. But I couldn’t see how to generate the whole collection; and doing an infinite loop seemed wrong somehow–the idea of finding the analog of generators in haskell hadn’t occurred to me. Also, probably not idiomatic haskell anyway.

The proper way to express all primes is to recurse, by calling $f$, within its definition, on the residual set. This elegant solution is actually shown ( at the time of this post was written ) on the upper right of the *haskell.org* landing page. Let , then

When $S$ is infinite, the $RHS$ is also infinite. However the infinite recursion is never realized because of lazy evaluation. In particular the set of all primes is expressed by . To me this recursive definition is a sort of set builder notation. The listing below is the Haskell implementation of $f$, declaration of all primes, and a request for a finite subset of $primes$.

```
Prelude> filterPrime (p:xs) = p : filterPrime [x | x <- xs, (mod x p) /= 0]
Prelude> primes = filterPrime [2..]
Prelude> take 30 primes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113]
```