Problem 45 in Ruby.