127
|
1 |
// Part 1 about the 3n+1 conjecture
|
|
2 |
//==================================
|
167
|
3 |
package CW6a
|
|
4 |
|
126
|
5 |
|
127
|
6 |
object CW6a {
|
126
|
7 |
|
127
|
8 |
def collatz(n: Long): Long =
|
|
9 |
if (n == 1) 1 else
|
|
10 |
if (n % 2 == 0) 1 + collatz(n / 2) else
|
|
11 |
1 + collatz(3 * n + 1)
|
126
|
12 |
|
|
13 |
|
127
|
14 |
def collatz_max(bnd: Long): (Long, Long) = {
|
|
15 |
val all = for (i <- (1 to bnd.toInt).toList) yield collatz(i)
|
126
|
16 |
val max = all.max
|
127
|
17 |
(max, all.indexOf(max) + 1)
|
126
|
18 |
}
|
|
19 |
|
|
20 |
|
127
|
21 |
}
|
126
|
22 |
|
167
|
23 |
|
171
|
24 |
def Collat(n: Long, r: Int = 1) : Int =
|
|
25 |
if (n == 1) r else
|
|
26 |
if (n % 2 == 0) Collat(n / 2 , r + 1) else
|
|
27 |
Collat (3 * n + 1 , r + 1)
|
|
28 |
|
|
29 |
(1 to 10).map(collatz(_))
|