Yesterday, while I was reading a book on Erlang, I’ve got this idea to devise and implement prime numbers finding algorithm without looking up anything. So I did. But then figured my friends may also want to get their feet wet and so I tweeted about the “challenge” and got 3 submissions (2 in Ruby and 1 in Erlang). Later, I discovered that a couple more would have wanted to try but never saw my tweet. Well, it’s a shame.
In this post I’d like to share the submissions and figure who’s the winner. I need to say upfront that I’m the looser. I took it all lightly and was punished for that. :)
Here are the submissions in the order of appearance:
- @dpskftw - Gist (Ruby)
- @marsbomber - Gist (Ruby)
- @codyrioux - Gist (Erlang)
- @spyromus - Gist (Erlang)
Note: I didn’t check the second submission by @dpskftw – the one that’s based on the built-in Ruby check for primes.
Every version returned correct results for the upper boundary of 100k (except my own which has never finished due to innovative algo I suppose). The times however were different. I figured, it doesn’t make much sense to judge by the code size, so I’ll give you the winners by speed. Here’s the list where I show the times of three consecutive runs and an average:
- 1st place: @dpskftw - 0.077 / 0.082 / 0.083 - 0.081 (Ruby 1.8.7), and 0.097 (Ruby 1.9.3)
- 2nd place: @marsbomber - 2.648 / 2.565 / 2.612 - 2.608 (Ruby 1.8.7) and 0.815 (Ruby 1.9.3)
- 3rd place: @codyrioux - 3.232 / 3.128 / 3.111 - 3.157
Congratulations, and Happy New Year everyone!
P.S. What I’d like to do now is to take @dpskftw’s solution and relay it onto Erlang to see if the times can be improved.