solved problem 22 in ruby