Solution for problem 5. Quickest solution yet. :)