testing1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 06 Nov 2019 00:36:45 +0000
changeset 314 21b52310bd8b
parent 281 87b9e3e2c1a7
child 320 cdfb2ce30a3d
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
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    10
/*
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    11
 * def collatz(n: Long): Long =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    12
  if (n == 1) 0 else
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    13
    if (n % 2 == 0) 1 + collatz(n / 2) else 
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    14
      1 + collatz(3 * n + 1)
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    15
*/
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    17
def collatz_max(bnd: Long): (Long, Long) = {
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    18
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    19
  all.maxBy(_._1)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
}
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    22
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    23
/* some test cases
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    24
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
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
for (bnd <- bnds) {
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    27
  val (steps, max) = collatz_max(bnd)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    28
  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
    29
}
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 197
diff changeset
    31
*/
171
4c9497ab5caa updated
Christian Urban <urbanc@in.tum.de>
parents: 167
diff changeset
    32
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    33
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    34
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    35
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    36
def collatz(n: Long) : Long = {
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    37
    if (n == 1) {
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    38
        1L
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    39
    } else {
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    40
        if (n % 2 == 0) {
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    41
            collatz(n/2) + 1
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    42
        } else {
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    43
            collatz((n*3)+1) + 1
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    44
        }
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    45
    }
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    46
}
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    47
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    48
}
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    49
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    50
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    51