solved problem 46 in ruby