Solve problem 4 using Scala.