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