core_templates1/collatz.scala
changeset 428 cdfa6a293453
parent 396 3ffe978a5664
child 429 126d0e47ac85
equal deleted inserted replaced
427:6e93040e3378 428:cdfa6a293453
     1 // Core Part 1 about the 3n+1 conjecture
     1 // Core Part 1 about the 3n+1 conjecture
     2 //============================================
     2 //============================================
     3 
     3 
     4 object C1 {
     4 object C1 {
     5 
     5 
     6 //(1) Complete the collatz function below. It should
     6 // ADD YOUR CODE BELOW
     7 //    recursively calculate the number of steps needed 
     7 //======================
     8 //    until the collatz series reaches the number 1.
       
     9 //    If needed, you can use an auxiliary function that
       
    10 //    performs the recursion. The function should expect
       
    11 //    arguments in the range of 1 to 1 Million.
       
    12 
     8 
       
     9 
       
    10 //(1) 
    13 def collatz(n: Long) : Long = ???
    11 def collatz(n: Long) : Long = ???
    14 
    12 
    15 
    13 
    16 //(2) Complete the collatz_max function below. It should
    14 //(2) 
    17 //    calculate how many steps are needed for each number 
       
    18 //    from 1 up to a bound and then calculate the maximum number of
       
    19 //    steps and the corresponding number that needs that many 
       
    20 //    steps. Again, you should expect bounds in the range of 1
       
    21 //    up to 1 Million. The first component of the pair is
       
    22 //    the maximum number of steps and the second is the 
       
    23 //    corresponding number.
       
    24 
       
    25 def collatz_max(bnd: Long) : (Long, Long) = ???
    15 def collatz_max(bnd: Long) : (Long, Long) = ???
    26 
    16 
    27 //(3) Implement a function that calculates the last_odd
    17 //(3)
    28 //    number in a collatz series.  For this implement an
       
    29 //    is_pow_of_two function which tests whether a number 
       
    30 //    is a power of two. The function is_hard calculates 
       
    31 //    whether 3n + 1 is a power of two. Again you can
       
    32 //    assume the input ranges between 1 and 1 Million,
       
    33 //    and also assume that the input of last_odd will not 
       
    34 //    be a power of 2.
       
    35 
       
    36 def is_pow_of_two(n: Long) : Boolean = ???
    18 def is_pow_of_two(n: Long) : Boolean = ???
    37 
    19 
    38 def is_hard(n: Long) : Boolean = ???
    20 def is_hard(n: Long) : Boolean = ???
    39 
    21 
    40 def last_odd(n: Long) : Long = ???
    22 def last_odd(n: Long) : Long = ???