Solved #8 in ruby.