core_testing1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 433 6af86ba1208f
child 472 6a77c260c8a5
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
     1
import scala.annotation.tailrec
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
     2
// Core Part 1 about the 3n+1 conjecture
433
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
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     5
object C1 {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     6
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
     7
@tailrec
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
     8
private def collatz(n: Long, steps: Long = 0): Long = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
     9
  if (n == 1) steps
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    10
  else if (n % 2 == 0) collatz(n / 2, steps + 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    11
  else collatz(n * 3 + 1, steps + 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    12
}
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    14
def collatz_max(upper: Long): (Long, Long) = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    15
  (1L to upper).map(n => (collatz(n), n)).maxBy(_._1)
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    16
}
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    19
private def is_pow_of_two(n: Long) : Boolean = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    20
  (n & (n - 1)) == 0
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    21
}
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    22
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    23
private def is_hard(n: Long) : Boolean = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    24
  is_pow_of_two(3 * n + 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    25
}
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    26
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    27
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    28
private def last_odd(n: Long): Long = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    29
  (1L to n).filter(is_hard).max
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 433
diff changeset
    30
}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    31
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    32
}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    33
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    34
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    35
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    36
// This template code is subject to copyright 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    37
// by King's College London, 2022. Do not 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    38
// make the template code public in any shape 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    39
// or form, and do not exchange it with other 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    40
// students under any circumstance.