author | Christian Urban <urbanc@in.tum.de> |
Tue, 07 Nov 2017 13:08:18 +0000 | |
changeset 127 | b4def82f3f9f |
parent 126 | c40f364d87eb |
permissions | -rw-r--r-- |
127 | 1 |
// Part 1 about the 3n+1 conjecture |
2 |
//================================== |
|
126 | 3 |
|
127 | 4 |
object CW6a { |
126 | 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) |
|
126 | 10 |
|
11 |
||
127 | 12 |
def collatz_max(bnd: Long): (Long, Long) = { |
13 |
val all = for (i <- (1 to bnd.toInt).toList) yield collatz(i) |
|
126 | 14 |
val max = all.max |
127 | 15 |
(max, all.indexOf(max) + 1) |
126 | 16 |
} |
17 |
||
18 |
||
127 | 19 |
} |
126 | 20 |