Problem 31: added Scala solution