Solved problem 17 in ruby