Solved #5 in ruby.