equal
deleted
inserted
replaced
5 // > scala -d collatz.jar collatz.scala |
5 // > scala -d collatz.jar collatz.scala |
6 |
6 |
7 object CW6a { |
7 object CW6a { |
8 |
8 |
9 |
9 |
10 def collatz(n: Long): Long = |
10 /* |
|
11 * def collatz(n: Long): Long = |
11 if (n == 1) 0 else |
12 if (n == 1) 0 else |
12 if (n % 2 == 0) 1 + collatz(n / 2) else |
13 if (n % 2 == 0) 1 + collatz(n / 2) else |
13 1 + collatz(3 * n + 1) |
14 1 + collatz(3 * n + 1) |
14 |
15 */ |
15 |
16 |
16 def collatz_max(bnd: Long): (Long, Long) = { |
17 def collatz_max(bnd: Long): (Long, Long) = { |
17 val all = for (i <- (1L to bnd)) yield (collatz(i), i) |
18 val all = for (i <- (1L to bnd)) yield (collatz(i), i) |
18 all.maxBy(_._1) |
19 all.maxBy(_._1) |
19 } |
20 } |
28 } |
29 } |
29 |
30 |
30 */ |
31 */ |
31 |
32 |
32 |
33 |
|
34 |
|
35 |
|
36 def collatz(n: Long) : Long = { |
|
37 if (n == 1) { |
|
38 1L |
|
39 } else { |
|
40 if (n % 2 == 0) { |
|
41 collatz(n/2) + 1 |
|
42 } else { |
|
43 collatz((n*3)+1) + 1 |
|
44 } |
|
45 } |
|
46 } |
|
47 |
33 } |
48 } |
34 |
49 |
35 |
50 |
36 |
51 |