testing1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 02 Feb 2019 13:35:07 +0000
changeset 262 21c69dc3dbf7
parent 199 54befaf23648
child 281 87b9e3e2c1a7
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
     1
// Part 1 about the 3n+1 conjecture
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
     2
//==================================
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
     3
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
     5
//object CW6a { // for purposes of generating a jar
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
     7
def collatz(n: Long): Long =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
     8
  if (n == 1) 0 else
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
     9
    if (n % 2 == 0) 1 + collatz(n / 2) else 
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    10
      1 + collatz(3 * n + 1)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    13
def collatz_max(bnd: Long): (Long, Long) = {
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    14
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    15
  all.maxBy(_._1)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    18
/* some test cases
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    19
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    21
for (bnd <- bnds) {
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    22
  val (steps, max) = collatz_max(bnd)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    23
  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    24
}
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    26
*/
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    27
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    28
//}