solved problem 38 in ruby