Replace recursion with loop

With recursion, benched at about 69000ns.
With loop, benched at about 42000ns.

This is already an imperative algorithm as we have implemented it, so we might as well go all the way and take what benefit there is to be had.