solutions1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 02 Nov 2019 15:11:30 +0000
changeset 305 b387e925d3c2
parent 282 2d290b79fc73
child 320 90aed247c8cf
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
282
2d290b79fc73 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     1
// Basic Part about the 3n+1 conjecture
208
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
266
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     4
// generate jar with
31e5218f43de updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     5
//   > scala -d collatz.jar  collatz.scala
208
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
object CW6a { // for purposes of generating a jar
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def collatz(n: Long): Long =
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  if (n == 1) 0 else
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    if (n % 2 == 0) 1 + collatz(n / 2) else 
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
      1 + collatz(3 * n + 1)
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def collatz_max(bnd: Long): (Long, Long) = {
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  all.maxBy(_._1)
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
}
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
/* some test cases
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
for (bnd <- bnds) {
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  val (steps, max) = collatz_max(bnd)
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
*/
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
ab0ccbc8271f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}