Problem 59 in ruby