Solve problem 22 using Scala.