Added third solution for this, based on a friend's clojure solution.