edited by
12,946 views
49 votes
49 votes

The function f is defined as follows:

int f (int n) {
    if (n <= 1) return 1;
    else if (n % 2  ==  0) return f(n/2);
    else return f(3n - 1);
}

Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.

  1. The function $f$ terminates for finitely many different values of $n \geq 1$.
  2. The function $f$ terminates for infinitely many different values of $n \geq 1$.
  3. The function $f$ does not terminate for finitely many different values of $n \geq 1$.
  4. The function $f$ does not terminate for infinitely many different values of $n \geq 1$.

Which one of the following options is true of the above?

  1. i and iii
  2. i and iv
  3. ii and iii
  4. ii and iv
edited by

5 Answers

Best answer
76 votes
76 votes

The function terminates for all powers of $2$ (which is infinite), hence (i) is false and (ii) is TRUE.

Let $n = 5. $
Now, recursive calls will go like $5 - 14 - 7 - 20 - 10 - 5 -$

And this goes into infinite recursion. And if we multiply $5$ with any power of $2$, also result will be infinite recursion. Since, there are infinite powers of $2$ possible, there are infinite recursions possible (even considering this case only). So, (iv) is TRUE and (iii) is false.

So, correct answer is (D).

edited by
2 votes
2 votes
The function is terminated for any value of n which is in power of 2. There are infinite nos, which are power of 2. Therefore (ii) is correct.

Now, take n= 10 , the function call will go in loop for value of n = 5, 14,7,20,10,5,14.......so on.

The above pattern will repeat for numbers 10, 20,40,80,160,320,640.... Infinity.

Therefore (iv) is also true.

Hence, answer is D
1 votes
1 votes

Recursion looks like as infinite recursion so simply option (ii) and (iv) Option D

For self checking take function value f(5).

0 votes
0 votes
For power of 2 option (ii) is true and for other No's option (iv) is correct so answer is D
Answer:

Related questions