solutions1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 30 Oct 2019 14:27:54 +0000
changeset 291 5372c5a538d7
parent 282 ec9773fe1dc0
child 320 cdfb2ce30a3d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
282
ec9773fe1dc0 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     1
// Basic Part about the 3n+1 conjecture
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//==================================
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     4
// generate jar with
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 208
diff changeset
     5
//   > scala -d collatz.jar  collatz.scala
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
object CW6a { // for purposes of generating a jar
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def collatz(n: Long): Long =
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  if (n == 1) 0 else
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    if (n % 2 == 0) 1 + collatz(n / 2) else 
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
      1 + collatz(3 * n + 1)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def collatz_max(bnd: Long): (Long, Long) = {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  all.maxBy(_._1)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
/* some test cases
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
for (bnd <- bnds) {
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  val (steps, max) = collatz_max(bnd)
f8883f8a14ad 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}")
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
*/
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}