awesome if somewhat slow solution to problem 10 in ruby