testing1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 03 Dec 2019 01:22:16 +0000
changeset 326 7d983ee99fcc
parent 323 93b6c16dded8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
320
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     1
object CW6a {
167
1bbd4db36151 updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
     2
320
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     3
//(1) Complete the collatz function below. It should
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     4
//    recursively calculate the number of steps needed 
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     5
//    until the collatz series reaches the number 1.
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     6
//    If needed, you can use an auxiliary function that
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     7
//    performs the recursion. The function should expect
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     8
//    arguments in the range of 1 to 1 Million.
323
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
     9
def stepsCounter(n: Long, s: Long) : Long = n match{
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    10
    case 1 => s
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    11
    case n if(n%2==0) => stepsCounter(n/2,s+1)
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    12
    case _ => stepsCounter(3*n+1, s+1)
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    13
}
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    14
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    15
def collatz(n: Long) : Long = n match {
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    16
    case n if(n>0) => stepsCounter(n,0)
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    17
    case n if(n<=0) => stepsCounter(1,0)
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    18
}
281
32dfd2ca577b updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    19
126
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
320
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    21
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    22
//(2) Complete the collatz_max function below. It should
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    23
//    calculate how many steps are needed for each number 
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    24
//    from 1 up to a bound and then calculate the maximum number of
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    25
//    steps and the corresponding number that needs that many 
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    26
//    steps. Again, you should expect bounds in the range of 1
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    27
//    up to 1 Million. The first component of the pair is
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    28
//    the maximum number of steps and the second is the 
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    29
//    corresponding number.
90aed247c8cf updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    30
323
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    31
def collatz_max(bnd: Long) : (Long, Long) =  {
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    32
    val allCollatz = for(i<-1L until bnd) yield collatz(i)
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    33
    val pair = (allCollatz.max, (allCollatz.indexOf(allCollatz.max) +1).toLong)
93b6c16dded8 updated
Christian Urban <urbanc@in.tum.de>
parents: 320
diff changeset
    34
    pair
126
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
}
2069054c2cac updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
127
01d522ba48d4 updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    37
}