solved problem 9 in ruby