Solved Problem 4.

My solution is remarkably brutish, actually, and I suspect there is a time efficient approach. This was remarkably easy to write in Clojure, though, and computes very quickly, so I guess I can't really complain.