testing1/collatz.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 19 Nov 2019 00:40:27 +0000
changeset 320 cdfb2ce30a3d
parent 314 21b52310bd8b
child 323 1f8005b4cdf6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     1
object CW6a {
167
349d706586ef updated
Christian Urban <urbanc@in.tum.de>
parents: 144
diff changeset
     2
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     3
//(1) Complete the collatz function below. It should
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     4
//    recursively calculate the number of steps needed 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     5
//    until the collatz series reaches the number 1.
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     6
//    If needed, you can use an auxiliary function that
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     7
//    performs the recursion. The function should expect
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
     8
//    arguments in the range of 1 to 1 Million.
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
     9
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    11
// def collatz(n: Long) : Long = {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    12
//     if (n == 1) 1 //else
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    13
//     // if (n % 2 == 0) {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    14
//     //     collatz(n/2)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    15
//     //     steps + 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    16
//     // } //else
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    17
//     // if (n % 2 != 0) {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    18
//     //     collatz((3 * n) + 1)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    19
//     //     steps + 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    20
//     // }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    21
// }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    22
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    23
// val steps: Long = 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    24
// val lst = List()
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    25
// def collatz(n: Long) : Long = {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    26
//     if  (n == 1) { steps + 1 }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    27
//     else if (n % 2 == 0) { 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    28
//         collatz(n/2);
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    29
//     }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    30
//     else { 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    31
//         collatz((3 * n) + 1);
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    32
//     }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    33
//     steps + 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    34
// } 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    35
// collatz(6)
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
320
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    37
def collatz(n: Long, list: List[Long] = List()): Long = {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    38
    if (n == 1) {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    39
            n :: list
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    40
            list.size.toLong
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    41
    }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    42
    else if (n % 2 == 0) {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    43
        collatz(n / 2, n :: list)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    44
    }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    45
    else {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    46
        collatz((3 * n) + 1, n :: list)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    47
    }
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    48
}   
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    49
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    50
val test = collatz(6)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    51
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    52
//(2) Complete the collatz_max function below. It should
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    53
//    calculate how many steps are needed for each number 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    54
//    from 1 up to a bound and then calculate the maximum number of
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    55
//    steps and the corresponding number that needs that many 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    56
//    steps. Again, you should expect bounds in the range of 1
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    57
//    up to 1 Million. The first component of the pair is
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    58
//    the maximum number of steps and the second is the 
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    59
//    corresponding number.
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    60
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    61
//def collatz_max(bnd: Long) : (Long, Long) = ...
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    62
def collatz_max(bnd: Long) : (Long, Long) = {
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    63
    val stepsTable = for (n <- (1 to bnd.toInt).toList) yield (collatz(n), n.toLong)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    64
    //println(stepsTable)
cdfb2ce30a3d updated
Christian Urban <urbanc@in.tum.de>
parents: 314
diff changeset
    65
    stepsTable.max
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    68
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 126
diff changeset
    69
}
126
c40f364d87eb updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70