// Core Part 1 about the 3n+1 conjecture
//============================================

object C1 {

// ADD YOUR CODE BELOW
//======================

// test1 7 Nov
// test2
// test3
// test4


//(1) 
def collatz(n: Long) : Long = 
  if (n == 1) 0 else
    if (n % 2 == 0) 1 + collatz(n / 2) else 
      1 + collatz(3 * n + 1)


//(2) 
//def collatz_max(bnd: Long) : (Long, Long) = {
//  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
//  all.maxBy(_._1)
//}

def collatz_max(bnd: Long): (Long, Long) = {
  val all = for (i <- (1L to bnd)) yield (collatz(i), i)
  all.maxBy(_._1)
}



//(3)

def is_pow_of_two(n: Long) : Boolean = (n & (n - 1)) == 0

def is_hard(n: Long) : Boolean = is_pow_of_two(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.
