solved problem 17 in ruby