solved problem 23 in ruby