| author | Christian Urban <urbanc@in.tum.de> | 
| Thu, 02 Nov 2017 14:47:55 +0000 | |
| changeset 123 | 006f71e905a1 | 
| parent 64 | d6f97b562424 | 
| child 127 | 01d522ba48d4 | 
| 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 | |
| 123 | 4 | |
| 5 | ||
| 17 | 6 | //(1) Complete the collatz function below. It should | 
| 7 | // recursively calculate the number of steps needed | |
| 8 | // until the collatz series reaches the number 1. | |
| 9 | // If needed you can use an auxilary function that | |
| 10 | // performs the recursion. The function should expect | |
| 24 
66b97f9a40f8
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
18diff
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 | |
| 17 | 13 | def collatz(n: Long): List[Long] = | 
| 14 | if (n == 1) List(1) else | |
| 15 | if (n % 2 == 0) (n::collatz(n / 2)) else | |
| 16 | (n::collatz(3 * n + 1)) | |
| 15 | 17 | |
| 11 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 18 | |
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 19 | // an alternative that calculates the steps directly | 
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 20 | def collatz1(n: Long): Int = | 
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 21 | if (n == 1) 1 else | 
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 22 | 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 | 23 | (1 + collatz1(3 * n + 1)) | 
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 24 | |
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 25 | |
| 17 | 26 | //(2) Complete the collatz bound function below. It should | 
| 27 | // calculuate how many steps are needed for each number | |
| 28 | // from 1 upto a bound and return the maximum number of | |
| 18 | 29 | // steps and the corresponding number that needs that many | 
| 17 | 30 | // steps. You should expect bounds in the range of 1 | 
| 18 | 31 | // upto 1 million. | 
| 17 | 32 | |
| 18 | 33 | def collatz_max(bnd: Int): (Int, Int) = {
 | 
| 34 | val all = for (i <- (1 to bnd).toList) yield collatz(i).length | |
| 35 | val max = all.max | |
| 36 | (all.indexOf(max) + 1, max) | |
| 11 
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 | |
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 39 | |
| 17 | 40 | // some testing harness | 
| 123 | 41 | val bnds = List(2, 10, 100, 1000, 10000, 100000, 77000, 90000, 1000000, 5000000) | 
| 11 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 42 | |
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 43 | for (bnd <- bnds) {
 | 
| 18 | 44 | val (max, steps) = collatz_max(bnd) | 
| 45 |   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 | 46 | } | 
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 47 | |
| 18 | 48 | |
| 49 | //val all = for (i <- (1 to 100000).toList) yield collatz1(i) | |
| 50 | //println(all.sorted.reverse.take(10)) | |
| 123 | 51 | |
| 52 | ||
| 53 |