solved problem 3 in ruby