ruby version of problem 5