solved problem 6 in ruby