| 401 |      1 | // Core Part 1 about the 3n+1 conjecture
 | 
| 430 |      2 | //============================================
 | 
|  |      3 | 
 | 
|  |      4 | object C1 {
 | 
|  |      5 | 
 | 
| 469 |      6 | def collatz(n: Long): Long =
 | 
|  |      7 |   if (n == 1) 0 else
 | 
|  |      8 |     if (n % 2 == 0) 1 + collatz(n / 2) else 
 | 
|  |      9 |       1 + collatz(3 * n + 1)
 | 
| 208 |     10 | 
 | 
|  |     11 | 
 | 
| 469 |     12 | def collatz_max(bnd: Long): (Long, Long) = {
 | 
|  |     13 |   val all = for (i <- (1L to bnd)) yield (collatz(i), i)
 | 
|  |     14 |   all.maxBy(_._1)
 | 
| 460 |     15 | }
 | 
| 363 |     16 | 
 | 
| 469 |     17 | def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
 | 
|  |     18 | 
 | 
|  |     19 | def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
 | 
| 363 |     20 | 
 | 
| 469 |     21 | def last_odd(n: Long) : Long = 
 | 
|  |     22 |   if (is_hard(n)) n else
 | 
|  |     23 |     if (n % 2 == 0) last_odd(n / 2) else 
 | 
|  |     24 |       last_odd(3 * n + 1)
 | 
| 335 |     25 | 
 | 
| 363 |     26 | }
 | 
| 335 |     27 | 
 | 
|  |     28 | 
 | 
|  |     29 | 
 | 
| 430 |     30 | // This template code is subject to copyright 
 | 
|  |     31 | // by King's College London, 2022. Do not 
 | 
|  |     32 | // make the template code public in any shape 
 | 
|  |     33 | // or form, and do not exchange it with other 
 | 
|  |     34 | // students under any circumstance.
 |