pre_testing1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 17 Nov 2020 00:34:55 +0000
changeset 362 1bde878ba6c9
parent 360 e45d2890749d
child 363 e5c1d69cffa4
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     1
object CW6a {
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     3
//(1) Complete the collatz function below. It should
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     4
//    recursively calculate the number of steps needed
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     5
//    until the collatz series reaches the number 1.
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     6
//    If needed, you can use an auxiliary function that
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     7
//    performs the recursion. The function should expect
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
     8
//    arguments in the range of 1 to 1 Million.
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    10
def collatz(n: Long) : Long =
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    11
    if ( n == 1) 1;
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    12
    else if (n % 2 == 0) 1 + collatz( n / 2);
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    13
    else 1 + collatz( n * 3 + 1);
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    16
//(2) Complete the collatz_max function below. It should
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    17
//    calculate how many steps are needed for each number
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    18
//    from 1 up to a bound and then calculate the maximum number of
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    19
//    steps and the corresponding number that needs that many
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    20
//    steps. Again, you should expect bounds in the range of 1
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    21
//    up to 1 Million. The first component of the pair is
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    22
//    the maximum number of steps and the second is the
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    23
//    corresponding number.
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    25
def collatz_max(bnd: Long) : (Long, Long) =
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    26
     ((1.toLong to bnd).toList.map
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    27
        (n => collatz(n)).max ,
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    28
            (1.toLong to bnd).toList.map
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    29
                (n => collatz(n)).indexOf((1.toLong to bnd).toList.map
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    30
                    (n => collatz(n)).max) + 1);
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    32
//(3) Implement a function that calculates the last_odd
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    33
//    number in a collatz series.  For this implement an
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    34
//    is_pow_of_two function which tests whether a number
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    35
//    is a power of two. The function is_hard calculates
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    36
//    whether 3n + 1 is a power of two. Again you can
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    37
//    assume the input ranges between 1 and 1 Million,
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    38
//    and also assume that the input of last_odd will not
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    39
//    be a power of 2.
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    40
//idk
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    41
 def is_pow_of_two(n: Long) : Boolean =
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    42
    if ( n & ( n - 1) == 0) true;
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    43
    else false;
208
f8883f8a14ad updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    45
def is_hard(n: Long) : Boolean =
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    46
    if ( (3*n + 1) & 3*n == 0) true;
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    47
    else false;
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    48
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    49
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    50
def last_odd(n: Long) : Long = ???
335
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    51
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    52
7e00d2b13b04 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 320
diff changeset
    53
362
1bde878ba6c9 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 360
diff changeset
    54
}