progs/collatz2.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 24 Sep 2021 23:56:31 +0100
changeset 839 7af70f9b30ab
parent 409 fb8ee3f58d19
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
409
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
def collatz(n: BigInt) : Boolean = n match {
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  case n if (n == 1) => true
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
  case n if (n % 2 == 0) => collatz(n / 2)  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
  case _ => collatz(3 * n + 1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
}