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