testing1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 02 Nov 2019 13:19:45 +0000
changeset 304 4a9d1153ccc5
parent 281 87b9e3e2c1a7
child 314 21b52310bd8b
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
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     4
// generate jar with
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     5
//   > scala -d collatz.jar  collatz.scala
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     7
object CW6a { 
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     8
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    10
def collatz(n: Long): Long =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    11
  if (n == 1) 0 else
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    12
    if (n % 2 == 0) 1 + collatz(n / 2) else 
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    13
      1 + collatz(3 * n + 1)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    16
def collatz_max(bnd: Long): (Long, Long) = {
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    17
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    18
  all.maxBy(_._1)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    21
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    22
/* some test cases
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    23
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    25
for (bnd <- bnds) {
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    26
  val (steps, max) = collatz_max(bnd)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    27
  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
    28
}
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    30
*/
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    31
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    32
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    33
}
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    34
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    35
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    36