The Sieve of Eratosthenes is a neat way to find all of the prime numbers below some number. If you do it on paper (and I suggest you do), you can easily find all primes below 100 within a few minutes. The challenge this week is to write a version in Clojure that lets you calculate all the primes below a given number n
.
The biggest challenge with this is that you might run into problems with stack overflows or speed. Make sure the algorithm can still run with n=100,000
.
I'd love to see your answers. Bonus points for creative and innovative solutions. I'll share the best next week. Fire up a REPL, try it out, and send me your answers. (Please note that, unless you tell me otherwise, I will share your name in the newsletter if you send me an answer.)
Been following #startup-in-a-month a bit https://www.twitch.tv/a_fry_ and got him interested in this sieve challenge. Of course fell down the rabbit hole myself again...
I have realized that most of the time spent with my submission (or, rather, a refined version of it) was with picking up the indices from the sieve. Tried a lot of things to speed it up and found that transducers is a bit faster, and that reducers is a bit faster than that, then that a plain
loop
is faster than that and then that using transients in the loop gains me some on top of that.Here's my latest, and so far fastest version: