problem 74 in Ruby
optimized, recursive solution to problem 74