Which of the following functions produces the same result as the factorial() function below?



``

int factorial(int num)

{

if (num <= 1)

{

return 1;

}

return num factorial(num - 1);

}

`



A.

`

int f(int n)

{

int res = 1;

for (int i = 1; i < n; i++)

res
= i;

return res;

}

`



B.

`

int f(int n)

{

int res = 1;

for (int i = 1; i <= n; i++)

res = i;

return res;

}

`



C.

`

int f(int n)

{

int res = 1;

for (int i = 2; i < n; i++)

res
= i;

return res;

}

``


DNone of these are correct.

Verified Answer :

  • The recursive factorial function computes \(n!\) for any non-negative integer \(n\).
  • Option 1 fails for \(n=2\) by returning 1 instead of 2.
  • Option 2 correctly initializes res=1 and multiplies by each integer from 1 up to \(n\), covering the base cases \(0!=1\) and \(1!=1\) and all subsequent values correctly.
  • Option 3 fails for \(n=2\) by returning 1 instead of 2.
  • The correct iterative function is:

\(\boxed{\text{int f(int n) \{ int res=1; for (int i = 1; i <= n; i++) res *= i; return res; \}}}\)

Explanation

  1. Analyze the Recursive Factorial Function

The recursive function factorial(int num) is defined such that for any non-negative integer num, it calculates the factorial num!. Specifically, if num <= 1, it returns 1 (covering both 0! and 1!), and for num > 1, it returns num factorial(num - 1). Let's test this with some values:

  • For \(n=0\): The function checks \(0 \leq 1\), so it returns 1.
  • For \(n=1\): The function checks \(1 \leq 1\), so it returns 1.
  • For \(n=2\): The function returns \(2 \times \text{factorial}(1) = 2 \times 1 = 2\).
  • For \(n=3\): The function returns \(3 \times \text{factorial}(2) = 3 \times 2 = 6\).
  • For \(n=4\): The function returns \(4 \times \text{factorial}(3) = 4 \times 6 = 24\).

The function correctly computes \(n!\) for all \(n \geq 0\).

  1. Evaluate the Second Iterative Option

Now we evaluate the iterative function provided as the second option:
``c
int f(int n) {
int res=1;
for (int i = 1; i <= n; i++)
res
= i;
return res;
}
`

  • For \(n=0\): The loop for (int i = 1; i <= 0; i++) does not execute as \(1 \leq 0\) is false. The initial value res = 1 is returned. (Correct, as \(0! = 1\))
  • For \(n=1\): The loop for (int i = 1; i <= 1; i++) executes for \(i=1\). res becomes \(1 \times 1 = 1\). (Correct, as \(1! = 1\))
  • For \(n=2\): The loop for (int i = 1; i <= 2; i++) executes for \(i=1, 2\). res becomes \(1 \times 1 \times 2 = 2\). (Correct, as \(2! = 2\))
  • For \(n=3\): The loop executes for \(i=1, 2, 3\). res becomes \(1 \times 1 \times 2 \times 3 = 6\). (Correct, as \(3! = 6\))

This function correctly computes \(n!\) for all \(n \geq 0\).

  1. Conclusion and Verification

We check the other options to ensure they do not also work or identify the correct one.

  • Option 1 (for (int i = 1; i < n; i++)): For \(n=2\), the loop executes only for \(i=1\), returning 1. Since \(2! = 2\), this is incorrect.
  • Option 3 (for (int i = 2; i < n; i++)): For \(n=2\), the loop condition 2 < 2` is false, returning 1. Since \(2! = 2\), this is also incorrect.

Since the second option consistently produces \(n!\) for all non-negative integers \(n\), it is the correct implementation.

Examples

In programming, understanding how recursion translates to iteration is a fundamental skill. For example, when building a data analysis tool that needs to calculate permutations or probability distributions, you might often compute factorials. While recursion is elegant and mathematically concise, it can lead to stack overflow errors for very large inputs. Converting recursive processes to iterative ones, as shown here, is a standard optimization technique used by developers to make software more robust, memory-efficient, and performant when handling complex calculations in real-time.