Problem 4-5 for "Ninety-Nine Scala Problems"