Prime number puzzle implemented in Scala