solved problem 39 again in ruby