| author | Christian Urban <urbanc@in.tum.de> | 
| Wed, 08 Nov 2017 20:27:56 +0000 | |
| changeset 135 | e59aebee9770 | 
| parent 127 | 01d522ba48d4 | 
| child 140 | aac534649b27 | 
| permissions | -rw-r--r-- | 
| 127 | 1 | // Part 1 about the 3n+1 conjecture | 
| 2 | //================================== | |
| 11 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 3 | |
| 127 | 4 | object CW6a {
 | 
| 123 | 5 | |
| 127 | 6 | def collatz(n: Long): Long = | 
| 7 | if (n == 1) 1 else | |
| 8 | if (n % 2 == 0) 1 + collatz(n / 2) else | |
| 9 | 1 + collatz(3 * n + 1) | |
| 15 | 10 | |
| 11 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 11 | |
| 127 | 12 | def collatz_max(bnd: Long): (Long, Long) = {
 | 
| 13 | val all = for (i <- (1 to bnd.toInt).toList) yield collatz(i) | |
| 18 | 14 | val max = all.max | 
| 127 | 15 | (max, all.indexOf(max) + 1) | 
| 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 | |
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 18 | |
| 127 | 19 | // some testing harness...5 Mio pushes the envelope | 
| 20 | ||
| 21 | val bnds = List(2, 10, 100, 1000, 10000, 100000, | |
| 22 | 77000, 90000, 1000000, 5000000) | |
| 11 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 23 | |
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 24 | for (bnd <- bnds) {
 | 
| 127 | 25 | val (steps, max) = collatz_max(bnd) | 
| 18 | 26 |   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 | 27 | } | 
| 
417869f65585
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 28 | |
| 18 | 29 | |
| 127 | 30 | } | 
| 123 | 31 |