Sieve Of Euler
February 25, 2011
Here’s our solution:
(define (euler n)
(let loop ((xs (range 3 n 2)) (ps '(2)))
(let ((p (car xs)))
(if (< n (* p p))
(append (reverse ps) xs)
(loop (minus (cdr xs) (sub-list n p xs))
(cons p ps))))))
Here xs
is the first list, (sub-list n p xs)
is the new list, and the subtraction is done by the minus
function:
(define (minus xs ys)
(let loop ((xs xs) (ys ys) (zs '()))
(cond ((null? ys) (append (reverse zs) xs))
((equal? (car xs) (car ys))
(loop (cdr xs) (cdr ys) zs))
(else (loop (cdr xs) ys (cons (car xs) zs))))))
We use auxiliary functions to compute the new list to be subtracted and to curry the multiply function:
(define (sub-list n p xs)
(let loop ((xs xs) (zs '()))
(let ((px (* p (car xs))))
(if (< n px) (reverse zs)
(loop (cdr xs) (cons px zs))))))
(define (times p) (lambda (x) (* p x)))
The three optimizations are included: We consider only odd numbers by specifying (range 3 n 2)
and starting with (2)
in the ps
result list. We stop early when (< n (* p p))
, appending the remaining list, which must necessarily contain only primes, to the primes already found. And we stop computing the new list when the product (* p x)
exceeds n
.
Here’s an example:
> (euler 100)
(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)
Though Euler’s sieve touches each number only once, it is slower than Eratosthenes’ sieve because it uses more operations and because so many intermediate lists must be created, then destroyed. The codepad solution shows a timing test in which Euler’s sieve was about twenty times slower than Eratosthenes’ sieve.
We used range
from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/dcEnoKkS.
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
Regarding the related problem of the number of primes less than N;
I wonder whether the fact that the Euler sieve only excludes numbers once;
can provide a basis for a general expression for the number of compound numbers less than N ?;
eg f(N) so that the number of primes less than N can be expressed as = N- f(N) ?