| author | Christian Urban <christian dot urban at kcl dot ac dot uk> | 
| Wed, 25 Jan 2017 14:51:09 +0000 | |
| changeset 106 | f4cd56d9cd4f | 
| parent 64 | d6f97b562424 | 
| child 123 | 006f71e905a1 | 
| 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  | 
|
| 
24
 
66b97f9a40f8
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
18 
diff
changeset
 | 
9  | 
// 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
 | 
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  | 
|
| 18 | 27  | 
// steps and the corresponding number that needs that many  | 
| 17 | 28  | 
// steps. You should expect bounds in the range of 1  | 
| 18 | 29  | 
// upto 1 million.  | 
| 17 | 30  | 
|
| 18 | 31  | 
def collatz_max(bnd: Int): (Int, Int) = {
 | 
32  | 
val all = for (i <- (1 to bnd).toList) yield collatz(i).length  | 
|
33  | 
val max = all.max  | 
|
34  | 
(all.indexOf(max) + 1, max)  | 
|
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
35  | 
}  | 
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
36  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
37  | 
|
| 17 | 38  | 
// some testing harness  | 
| 64 | 39  | 
val bnds = List(2, 10, 100, 1000, 10000, 100000, 77000, 90000, 1000000)  | 
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
40  | 
|
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
41  | 
for (bnd <- bnds) {
 | 
| 18 | 42  | 
val (max, steps) = collatz_max(bnd)  | 
43  | 
  println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
 | 
|
| 
11
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
44  | 
}  | 
| 
 
417869f65585
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents:  
diff
changeset
 | 
45  | 
|
| 18 | 46  | 
|
47  | 
//val all = for (i <- (1 to 100000).toList) yield collatz1(i)  | 
|
48  | 
//println(all.sorted.reverse.take(10))  |