solved problem 24 in ruby