Solve problem 37 using Scala.