author | Christian Urban <urbanc@in.tum.de> |
Tue, 08 Nov 2016 10:37:18 +0000 | |
changeset 17 | ecf83084e41d |
parent 15 | 52713e632ac0 |
child 18 | 87e55eb309ed |
permissions | -rw-r--r-- |
15 | 1 |
// Part 1 about the 3n+1 conceture |
2 |
//================================= |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
17 | 4 |
//(1) Complete the collatz function below. It should |
5 |
// recursively calculate the number of steps needed |
|
6 |
// until the collatz series reaches the number 1. |
|
7 |
// If needed you can use an auxilary function that |
|
8 |
// performs the recursion. The function should expect |
|
9 |
// arguments in the range of 1 to 10 Million. |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
|
17 | 11 |
def collatz(n: Long): List[Long] = |
12 |
if (n == 1) List(1) else |
|
13 |
if (n % 2 == 0) (n::collatz(n / 2)) else |
|
14 |
(n::collatz(3 * n + 1)) |
|
15 | 15 |
|
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
// an alternative that calculates the steps directly |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
def collatz1(n: Long): Int = |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
if (n == 1) 1 else |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
if (n % 2 == 0) (1 + collatz1(n / 2)) else |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
(1 + collatz1(3 * n + 1)) |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
|
17 | 24 |
//(2) Complete the collatz bound function below. It should |
25 |
// calculuate how many steps are needed for each number |
|
26 |
// from 1 upto a bound and return the maximum number of |
|
27 |
// steps. You should expect bounds in the range of 1 |
|
28 |
// upto 10 million. |
|
29 |
||
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
def collatz_max(bnd: Int): Int = { |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
(for (i <- 1 to bnd) yield collatz(i).length).max |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
|
17 | 35 |
// some testing harness |
11
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
val bnds = List(10, 100, 1000, 10000, 100000, 1000000, 10000000) |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
|
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
for (bnd <- bnds) { |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
val max = collatz_max(bnd) |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
println(s"In the range of 1 - ${bnd} the maximum steps are ${max}") |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
} |
417869f65585
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |