// Core Part 1 about the 3n+1 conjecture
//============================================
object C1 {
def collatz(n: Long): Long =
if (n == 1) 0 else
if (n % 2 == 0) 1 + collatz(n / 2) else
1 + collatz(3 * n + 1)
def collatz_max(bnd: Long): (Long, Long) = {
val all = for (i <- (1L to bnd)) yield (collatz(i), i)
all.maxBy(_._1)
}
def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
def last_odd(n: Long) : Long =
if (is_hard(n)) n else
if (n % 2 == 0) last_odd(n / 2) else
last_odd(3 * n + 1)
}
// This template code is subject to copyright
// by King's College London, 2022. Do not
// make the template code public in any shape
// or form, and do not exchange it with other
// students under any circumstance.