Solved problem 14 in ruby