solutions1/collatz.scala
changeset 335 7e00d2b13b04
parent 320 cdfb2ce30a3d
child 336 25d9c3b2bc99
equal deleted inserted replaced
334:841727e27252 335:7e00d2b13b04
    29   println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
    29   println(s"In the range of 1 - ${bnd} the number ${max} needs the maximum steps of ${steps}")
    30 }
    30 }
    31 
    31 
    32 */
    32 */
    33 
    33 
       
    34 def is_pow(n: Long) : Boolean = (n & (n - 1)) == 0
       
    35 
       
    36 def is_hard(n: Long) : Boolean = is_pow(3 * n + 1)
       
    37 
       
    38 def last_odd(n: Long) : Long = 
       
    39   if (is_hard(n)) n else
       
    40     if (n % 2 == 0) last_odd(n / 2) else 
       
    41       last_odd(3 * n + 1)
       
    42 
       
    43 
       
    44 //for (i <- 1 to 130) println(s"$i: ${last_odd(i)}")
       
    45 
    34 }
    46 }
       
    47 
       
    48 
       
    49