Factorial Using Recursion in DSA C# - Time & Space Complexity
We want to understand how the time to calculate a factorial grows as the number gets bigger.
How many steps does the computer take when using recursion to find factorial?
Analyze the time complexity of the following code snippet.
public int Factorial(int n)
{
if (n <= 1) return 1;
return n * Factorial(n - 1);
}
This code calculates the factorial of a number by calling itself with smaller numbers until it reaches 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call to Factorial with n-1.
- How many times: Exactly n times until reaching the base case.
Each time we call Factorial, it does one multiplication and calls itself with a smaller number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 multiplications and calls |
| 100 | About 100 multiplications and calls |
| 1000 | About 1000 multiplications and calls |
Pattern observation: The number of steps grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time to compute factorial grows in a straight line with the input number.
[X] Wrong: "Recursion makes factorial calculation take exponential time because it calls itself many times."
[OK] Correct: Each recursive call reduces n by 1 and happens only once per number, so calls grow linearly, not exponentially.
Understanding recursion time helps you explain how simple repeated steps add up, a key skill in many coding problems.
"What if we used a loop instead of recursion? How would the time complexity change?"
Practice
Solution
Step 1: Understand factorial definition
Factorial of a number n (written n!) is the product of all positive integers from 1 to n.Step 2: Compare options
Only The product of all positive integers up to that number correctly describes factorial as a product, not sum or division.Final Answer:
The product of all positive integers up to that number -> Option DQuick Check:
Factorial = product of integers [OK]
- Confusing factorial with sum
- Thinking factorial divides the number
- Assuming factorial doubles the number
Solution
Step 1: Identify factorial base case
By definition, 0! = 1, so the base case should return 1 when n is 0.Step 2: Check options
Only if (n == 0) return 1; returns 1 when n == 0, which is correct base case for recursion.Final Answer:
if (n == 0) return 1; -> Option AQuick Check:
Base case: 0! = 1 [OK]
- Using n == 1 as base case
- Returning wrong value in base case
- Not handling negative inputs
static int Factorial(int n) {
if (n == 0) return 1;
return n * Factorial(n - 1);
}
Console.WriteLine(Factorial(4));Solution
Step 1: Trace recursive calls for Factorial(4)
Factorial(4) = 4 * Factorial(3) Factorial(3) = 3 * Factorial(2) Factorial(2) = 2 * Factorial(1) Factorial(1) = 1 * Factorial(0) Factorial(0) = 1 (base case)Step 2: Calculate values bottom-up
Factorial(1) = 1 * 1 = 1 Factorial(2) = 2 * 1 = 2 Factorial(3) = 3 * 2 = 6 Factorial(4) = 4 * 6 = 24Final Answer:
24 -> Option AQuick Check:
4! = 24 [OK]
- Stopping recursion too early
- Adding instead of multiplying
- Misunderstanding base case
static int Factorial(int n) {
if (n == 1) return 1;
return n * Factorial(n);
}Solution
Step 1: Analyze recursive call
The function calls Factorial(n) inside itself without changing n, causing infinite recursion.Step 2: Identify fix
Recursive call should be Factorial(n - 1) to reduce n and reach base case.Final Answer:
Recursive call uses same n, causing infinite recursion -> Option CQuick Check:
Recursive call must reduce n [OK]
- Calling function with same argument
- Forgetting base case
- Wrong return type
static int Factorial(int n) {
if (n == 0) return 1;
return n * Factorial(n - 1);
}Solution
Step 1: Understand negative input handling
Factorial is undefined for negative numbers, so function should return -1 for n < 0.Step 2: Modify function
Add a check at start: if (n < 0) return -1; before base case to handle negatives properly.Final Answer:
Add if (n < 0) return -1; before base case -> Option BQuick Check:
Return -1 for negatives before recursion [OK]
- Changing base case incorrectly
- Not handling negatives at all
- Returning wrong value for negatives
Ready to prove it?
Pass the test and this pattern moves unknown
