## Sieve Of Euler

### February 25, 2011

The ancient Sieve of Eratosthenes that computes the list of prime numbers is inefficient in the sense that some composite numbers are struck out more than once; for instance, 21 is struck out by both 3 and 7. The great Swiss mathematician Leonhard Euler invented a sieve that strikes out each composite number exactly once, at the cost of some additional bookkeeping. It works like this:

First, make a list of numbers from 2, as large as you wish; call the maximum number

n.Second, extract the first number from the list, make a new list in which each element of the original list, including the first, is multiplied by the extracted first number.

Third, “subtract” the new list from the original, keeping in an output list only those numbers in the original list that do not appear in the new list.

Fourth, output the first number from the list, which is prime, and repeat the second, third and fourth steps on the reduced list excluding its first element, continuing until the input list is exhausted.

For example, start with the list 2 3 4 5 … 30. Then the new list is 4 6 8 10 … 60. Subtracting gives the list 2 3 5 7 9 … 29. Now 2 is prime and the process repeats on the list 3 5 7 9 … 29. At the next step, the new list is 9 15 21 27 … 87, subtracting gives the list 3 5 7 11 13 … 29, now 2 and 3 are prime and the process repeats on the list 5 7 11 13 … 29. Likewise for the primes 5 and 7, and since 7 · 7 > 30, the process stops, with the remaining list 11 13 17 19 23 29, so the complete list of primes less than 30 is 2 3 5 7 11 13 17 19 23 29.

Just as in the Sieve of Eratosthenes, you can speed up the Sieve of Euler by considering only odd numbers, by stopping once the first item in the list is greater than the square root of *n*, and by computing the new list in the second step only as far as *n*.

Your task is to write a program that implements the Sieve of Euler, then compare its performance to the Sieve of Eratosthenes. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

My Haskell solution (see http://bonsaicode.wordpress.com/2011/02/25/programming-praxis-sieve-of-euler/ for a version with comments):

[…] today’s Programming Praxis exercise, our goal is to implement the Sieve of Euler to generate primes. […]

Here’s my implementation of Euler’s Sieve:

Strictly speaking, it doesn’t build the second list and subtract—the best way I could find to do this was

with sets, but the time complexity was still larger than it neede to be—but it does filter out all multiples

of the first item once and only once. I tried to get a faster version going using iterators (kind of like lazy

lists), but it turned out to be slower. Eratosthenes still wins; see codepad for timing, where I

pit this against the Python Cookbook’s

`erat2()`

implementation.Unoptimized:

In ruby without any speedups …

My try in REXX

/*

Euler needed 0.010000 seconds for 25 primes in 100 numbers.

Eratosthenes needed 0.006000 seconds for 25 primes in 100 numbers.

*/

Here’s my implementation in C++ (see repository at https://bitbucket.org/mekajfire/prime-number-generators).

Rainer: I used REXX back in my OS/2 days, probably twenty years ago. I wrote a critique of REXX at http://groups.google.com/group/comp.lang.rexx/msg/7584cb0c6d4d2981. Is the language still active?

Hello Phil,

I used it professionally on the AS/400 and like to think of it as a great language for teaching. I will write you a private mail.

Best regards

Rainer

My Python Solution

[…] crible d’Euler, à la différence du crible d’Ératosthène, n’élimine les nombres composites […]

Without any optimizations, the Sieve of Eratosthenes is incredibly superior. Here’s the output of my program when obtaining the list of primes below 15,000:

Sieve of Eratosthenes ran in 0 s

Sieve of Euler ran in 4 s

Here’s the Java code for my implementation of the Sieve of Euler:

And here’s my crude implementation of the Sieve of Eratosthenes:

The complete implementation can be found here.

After spending way too much time on this task, my fastest version of the Sieve of Eratsthenes is consistently 2 to 3 times faster than my fastest Sieve of Euler.

On my HP 2GHz core2 duo laptop:

erat14(1e7) took about 1.2 seconds,

euler18(1e7) took about 3.4 seconds.

For reference, erat2(1e7) from the Python Cookbook took about 7.4 seconds.

Mine in F#

C++ implementation. Made sure to iterate over each element exactly once, which is the crux of this algorithm.

To achieve this I had to store the ‘non-deleted’ previous and next number of each number, at the end of each iteration. So, that in next time i can go the correct next in constant time.

https://github.com/puneetmnit/playground/blob/master/Algorithms/sieve_of_euler.cpp