problem 17 done in ruby