Solution to problem 53 in scala.