Solved problem 12 in ruby