core_testing1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 08 Dec 2022 17:53:08 +0000
changeset 452 4772b6166720
parent 433 6af86ba1208f
child 463 0315d9983cd0
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
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     6
// ADD YOUR CODE BELOW
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     7
//======================
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
     8
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
     9
// test1 7 Nov
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    10
// test2
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    11
// test3
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    12
// test4
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    13
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    15
//(1) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    16
def collatz(n: Long) : Long = 
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    17
  if (n == 1) 0 else
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    18
    if (n % 2 == 0) 1 + collatz(n / 2) else 
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    19
      1 + collatz(3 * n + 1)
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    21
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    22
//(2) 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    23
//def collatz_max(bnd: Long) : (Long, Long) = {
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    24
//  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    25
//  all.maxBy(_._1)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    26
//}
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    27
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    28
def collatz_max(bnd: Long): (Long, Long) = {
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    29
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    30
  all.maxBy(_._1)
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    31
}
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
401
9471c3b7ea02 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    34
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    35
//(3)
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    36
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    37
def is_pow_of_two(n: Long) : Boolean = (n & (n - 1)) == 0
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    38
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    39
def is_hard(n: Long) : Boolean = is_pow_of_two(3 * n + 1)
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    40
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    41
def last_odd(n: Long) : Long = if (is_hard(n)) n else
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    42
    if (n % 2 == 0) last_odd(n / 2) else 
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    43
      last_odd(3 * n + 1)
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    44
363
e5c1d69cffa4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 362
diff changeset
    45
}
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    46
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    47
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    48
433
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    49
// This template code is subject to copyright 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    50
// by King's College London, 2022. Do not 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    51
// make the template code public in any shape 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    52
// or form, and do not exchange it with other 
6af86ba1208f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 401
diff changeset
    53
// students under any circumstance.