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