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