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