415
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
// Core Part 1 about the 3n+1 conjecture
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
//============================================
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
4 |
object C1 {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
//(1) Complete the collatz function below. It should
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
// recursively calculate the number of steps needed
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
// until the collatz series reaches the number 1.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
// If needed, you can use an auxiliary function that
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
// performs the recursion. The function should expect
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
// arguments in the range of 1 to 1 Million.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
def collatz(n: Long) : Long = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
//(2) Complete the collatz_max function below. It should
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
// calculate how many steps are needed for each number
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
// from 1 up to a bound and then calculate the maximum number of
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
// steps and the corresponding number that needs that many
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
// steps. Again, you should expect bounds in the range of 1
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
// up to 1 Million. The first component of the pair is
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
// the maximum number of steps and the second is the
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
// corresponding number.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
def collatz_max(bnd: Long) : (Long, Long) = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
//(3) Implement a function that calculates the last_odd
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
// number in a collatz series. For this implement an
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
// is_pow_of_two function which tests whether a number
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
30 |
// is a power of two. The function is_hard calculates
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
31 |
// whether 3n + 1 is a power of two. Again you can
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
32 |
// assume the input ranges between 1 and 1 Million,
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
33 |
// and also assume that the input of last_odd will not
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
34 |
// be a power of 2.
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
35 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
36 |
def is_pow_of_two(n: Long) : Boolean = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
def is_hard(n: Long) : Boolean = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
def last_odd(n: Long) : Long = ???
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
42 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
44 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
|