Solve problem 12 using Scala.