I've updated my shockingly slow recursive version by making it do a better tail call. It now runs about as fast as Tom's recursive solution. I've also added a completely different approach that runs about as fast as Dave's solution. https://gist.github.com/evilstreak/7161191