I’m always interested in new technologies and languages. We’re using Scala at work, and I really quite like the language. I really do like static (inferred) types. But on the other hand, as an AI graduate Lisp holds a certain allure and is a language I’ve wanted to learn for a long time but somehow never really gotten around to. One (for me) silly roadblock has been the stupid names of functions like
car rather than
rest. Clojure actually fixes this deficiency in Lisp, so I decided to give it a go.
Since I didn’t have a particular project in mind I started out by trying to solve problems from Project Euler. The first 2 were simple, but for the third I needed a pretty fast prime generator. My first attempt was very simple and produced a correct sequence, but it is incredibly slow. Here it is:
The silliest thing about it is that it’s generating a list of all numbers, then filtering out the even ones, and testing all of those. That was easy to fix, and as you can see I learnt about the
# macro as well in the process. This version is about twice as fast as the above:
It still an incredibly naive implementation though, and about a gazillion times slower than the version by Christophe Grand. My understanding of Clojure was not deep enough to completely understand how that implementation works. But, I was able to learn some things (for example the
letfn construct) from it and I set out to explore an idea I had for a fast, lazy Sieve of Eratosthenes.
My idea was to keep a set containing prime multiples for fast checking, and a map where the keys are the primes found so far, and the values were the highest multiple of it in the set. The set would continually be pruned for values lower than the current candidate, and the set would be updated from the keys and values in the map. I even did micro-optimisations like tripling each prime’s value as the initial value in the set (because even values would never be candidates) and adding “its” prime to this value twice each time we subsequently updated the set. My last attempt is shown below.
It is a moderate improvement on my naive solutions, but it’s still damn slow! However, at this point I have learnt enough Clojure to actually understand Christophe’s version. And it is sublime! It accomplishes exactly my goals, but so much more elegantly and efficiently I hardly know where to start. So I won’t! Now, there’s not much I can do to improve the efficiency or elegance of his algorithm, but I can improve the run speed by making the map transient. By that minimal change Christophe’s algorithm was speeded up by a hefty factor: from 18 to 13 seconds to retrieve the 1M + 1 prime:
I think it’s fast enough for now, so now I’ll continue onto Euler Problem 4…