testing/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Sun, 05 Nov 2017 12:56:55 +0000
changeset 126 2069054c2cac
child 127 01d522ba48d4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
126
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about the 3n+1 conceture
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//=================================
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
//(1) Complete the collatz function below. It should
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
//    recursively calculate the number of steps needed 
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
//    until the collatz series reaches the number 1.
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
//    If needed you can use an auxilary function that
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//    performs the recursion. The function should expect
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
//    arguments in the range of 1 to 1 Million.
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
def collatz(n: Long): Int =
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  if (n == 1) 1 else
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    if (n % 2 == 0) (1 + collatz(n / 2)) else 
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
      (1 + collatz(3 * n + 1))
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
//(2)  Complete the collatz bound function below. It should
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
//     calculuate how many steps are needed for each number 
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
//     from 1 upto a bound and then produce the maximum number of
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
//     steps and the corresponding number that needs that many 
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
//     steps. You should expect bounds in the range of 1
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
//     upto 1 million. 
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
def collatz_max(bnd: Int): (Int, Int) = {
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  val all = for (i <- (1 to bnd).toList) yield collatz(i)
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
  val max = all.max
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  (all.indexOf(max) + 1, max)
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
}
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
// some testing harness
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
/*
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
val bnds = List(2, 10, 100, 1000, 10000, 100000, 77000, 90000, 1000000, 5000000)
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
for (bnd <- bnds) {
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  val (max, steps) = collatz_max(bnd)
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
}
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
*/
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
//val all = for (i <- (1 to 100000).toList) yield collatz1(i)
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
//println(all.sorted.reverse.take(10))
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47