solved problem 58 in ruby