progs/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 23 Sep 2023 22:26:52 +0100
changeset 926 42ecc3186944
parent 407 4b454a6d1814
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
407
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
def collatz(n: BigInt) : Boolean = {
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
  if (n == 1) true else 
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
  if (n % 2 == 0) collatz(n / 2) else 
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
  collatz(3 * n + 1)
4b454a6d1814 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
}