core_testing1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 26 Apr 2024 17:35:36 +0100
changeset 486 9c03b5e89a2a
parent 472 6a77c260c8a5
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
     1
// Core Part 1 about the 3n+1 conjecture
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     2
//============================================
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     3
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     4
object C1 {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     5
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
     6
def collatz(n: Long): Long =
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
     7
  if (n == 1) 0 else
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
     8
    if (n % 2 == 0) 1 + collatz(n / 2) else 
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
     9
      1 + collatz(3 * n + 1)
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    12
def collatz_max(bnd: Long): (Long, Long) = {
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    13
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    14
  all.maxBy(_._1)
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    15
}
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    16
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    17
def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    18
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    19
def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    20
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    21
def last_odd(n: Long) : Long = 
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    22
  if (is_hard(n)) n else
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    23
    if (n % 2 == 0) last_odd(n / 2) else 
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 463
diff changeset
    24
      last_odd(3 * n + 1)
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    25
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    26
}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    27
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    28
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    29
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    30
// This template code is subject to copyright 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    31
// by King's College London, 2022. Do not 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    32
// make the template code public in any shape 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    33
// or form, and do not exchange it with other 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    34
// students under any circumstance.