Solve problem 31 using Ruby.