solved problem 9 with ruby