pre_marking1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 28 Nov 2020 15:37:30 +0000
changeset 376 6cc36d0ef79e
parent 373 d29cd5883c7b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Basic Part about the 3n+1 conjecture
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//==================================
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// generate jar with
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//   > scala -d collatz.jar  collatz.scala
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
object CW6a { // for purposes of generating a jar
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
def collatz(n: Long): Long =
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  if (n == 1) 0 else
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
    if (n % 2 == 0) 1 + collatz(n / 2) else 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
      1 + collatz(3 * n + 1)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
def collatz_max(bnd: Long): (Long, Long) = {
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  all.maxBy(_._1)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
}
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
//collatz_max(1000000)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
//collatz_max(10000000)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
//collatz_max(100000000)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
/* some test cases
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
for (bnd <- bnds) {
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  val (steps, max) = collatz_max(bnd)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
}
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
*/
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
def last_odd(n: Long) : Long = 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
  if (is_hard(n)) n else
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
    if (n % 2 == 0) last_odd(n / 2) else 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
      last_odd(3 * n + 1)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
//for (i <- 130 to 10000) println(s"$i: ${last_odd(i)}")
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
//for (i <- 1 to 100) println(s"$i: ${collatz(i)}")
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
}
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50