Solved problem 48 in Ruby