Solve problem 4 in Ruby.