pre_templates1/collatz.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 02 Dec 2020 01:15:14 +0000
changeset 382 1bd800376e0c
parent 345 40657f9a4e4a
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
     1
// Preliminary Part about the 3n+1 conjecture
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
     2
//============================================
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
     4
object CW6a {
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     6
//(1) Complete the collatz function below. It should
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     7
//    recursively calculate the number of steps needed 
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
     8
//    until the collatz series reaches the number 1.
127
b4def82f3f9f updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     9
//    If needed, you can use an auxiliary function that
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    10
//    performs the recursion. The function should expect
24
66b97f9a40f8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 18
diff changeset
    11
//    arguments in the range of 1 to 1 Million.
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    13
def collatz(n: Long) : Long = ???
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    16
//(2) Complete the collatz_max function below. It should
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    17
//    calculate how many steps are needed for each number 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    18
//    from 1 up to a bound and then calculate the maximum number of
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    19
//    steps and the corresponding number that needs that many 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    20
//    steps. Again, you should expect bounds in the range of 1
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    21
//    up to 1 Million. The first component of the pair is
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    22
//    the maximum number of steps and the second is the 
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
    23
//    corresponding number.
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 11
diff changeset
    24
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    25
def collatz_max(bnd: Long) : (Long, Long) = ???
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    26
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    27
//(3) Implement a function that calculates the last_odd
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    28
//    number in a collatz series.  For this implement an
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    29
//    is_pow_of_two function which tests whether a number 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    30
//    is a power of two. The function is_hard calculates 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    31
//    whether 3n + 1 is a power of two. Again you can
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    32
//    assume the input ranges between 1 and 1 Million,
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    33
//    and also assume that the input of last_odd will not 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    34
//    be a power of 2.
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    35
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    36
def is_pow_of_two(n: Long) : Boolean = ???
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    37
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    38
def is_hard(n: Long) : Boolean = ???
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    39
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    40
def last_odd(n: Long) : Long = ???
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
    42
}
11
417869f65585 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43