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