problem 112 in Ruby