127
|
1 |
// Part 1 about the 3n+1 conjecture
|
|
2 |
//==================================
|
167
|
3 |
|
126
|
4 |
|
199
|
5 |
//object CW6a { // for purposes of generating a jar
|
126
|
6 |
|
127
|
7 |
def collatz(n: Long): Long =
|
199
|
8 |
if (n == 1) 0 else
|
127
|
9 |
if (n % 2 == 0) 1 + collatz(n / 2) else
|
|
10 |
1 + collatz(3 * n + 1)
|
126
|
11 |
|
|
12 |
|
127
|
13 |
def collatz_max(bnd: Long): (Long, Long) = {
|
199
|
14 |
val all = for (i <- (1L to bnd)) yield (collatz(i), i)
|
|
15 |
all.maxBy(_._1)
|
126
|
16 |
}
|
|
17 |
|
199
|
18 |
/* some test cases
|
|
19 |
val bnds = List(10, 100, 1000, 10000, 100000, 1000000)
|
126
|
20 |
|
199
|
21 |
for (bnd <- bnds) {
|
|
22 |
val (steps, max) = collatz_max(bnd)
|
|
23 |
println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
|
127
|
24 |
}
|
126
|
25 |
|
199
|
26 |
*/
|
171
|
27 |
|
199
|
28 |
//}
|