Problem 24 in Ruby.