testing1/collatz.scala
changeset 144 716042628398
parent 127 b4def82f3f9f
child 167 349d706586ef
equal deleted inserted replaced
143:11396c17cd8b 144:716042628398
       
     1 // Part 1 about the 3n+1 conjecture
       
     2 //==================================
       
     3 
       
     4 object CW6a {
       
     5 
       
     6 def collatz(n: Long): Long =
       
     7   if (n == 1) 1 else
       
     8     if (n % 2 == 0) 1 + collatz(n / 2) else 
       
     9       1 + collatz(3 * n + 1)
       
    10 
       
    11 
       
    12 def collatz_max(bnd: Long): (Long, Long) = {
       
    13   val all = for (i <- (1 to bnd.toInt).toList) yield collatz(i)
       
    14   val max = all.max
       
    15   (max, all.indexOf(max) + 1)
       
    16 }
       
    17 
       
    18 
       
    19 }
       
    20