Discover how a function can call itself to solve a problem step by step!
Why Factorial Using Recursion in DSA Typescript?
Imagine you want to find the factorial of a number, like 5! (which means 5 x 4 x 3 x 2 x 1). Doing this by hand or writing many lines of code to multiply each number one by one can be tiring and error-prone.
Manually multiplying each number takes a lot of time and can easily lead to mistakes, especially for bigger numbers. Writing repetitive code for each step is boring and not practical.
Recursion lets the function call itself to solve smaller parts of the problem until it reaches the simplest case. This way, the factorial calculation becomes clean, short, and easy to understand.
function factorial(n: number): number {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}function factorial(n: number): number {
if (n === 0) return 1;
return n * factorial(n - 1);
}Recursion enables solving complex problems by breaking them into simpler, repeatable steps that call themselves.
Calculating combinations in probability, where factorials are used to find how many ways to choose items from a group.
Manual multiplication is slow and error-prone.
Recursion simplifies repetitive tasks by self-calling.
Factorial is a classic example to learn recursion.
Practice
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as 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 other operations.Final Answer:
The product of all positive integers up to that number -> Option AQuick Check:
Factorial = product of integers [OK]
- Confusing factorial with sum
- Thinking factorial divides
- Mixing factorial with doubling
Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 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 is 0, which is correct base case for recursion.Final Answer:
if (n === 0) return 1; -> Option AQuick Check:
Base case: 0! = 1 [OK]
- Returning 0 for base case
- Using wrong base condition
- Returning n+1 incorrectly
function factorial(n: number): number {
if (n === 0) return 1;
return n * factorial(n - 1);
}
console.log(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 DQuick Check:
4! = 24 [OK]
- Stopping recursion too early
- Adding instead of multiplying
- Wrong base case
function factorial(n: number): number {
if (n === 1) return 1;
return n * factorial(n - 1);
}
console.log(factorial(0));Solution
Step 1: Analyze base case for input 0
Base case triggers only when n === 1, so factorial(0) calls factorial(-1), then factorial(-2), and so on infinitely.Step 2: Understand recursion behavior
Without base case for 0, recursion never stops for input 0, causing infinite recursion (stack overflow).Final Answer:
Infinite recursion for input 0 -> Option BQuick Check:
Missing base case for 0 causes infinite recursion [OK]
- Missing base case for 0
- Assuming 1 covers 0
- Ignoring negative inputs
Solution
Step 1: Understand factorial domain
Factorial is not defined for negative numbers, so function should handle negatives before recursion.Step 2: Add input validation
Adding a condition to return -1 (or error) for negative inputs prevents invalid recursion calls.Final Answer:
Add a check to return -1 for negative inputs before recursion -> Option CQuick Check:
Validate input before recursion to handle negatives [OK]
- Ignoring negative inputs
- Letting recursion run on negatives
- Returning wrong values for negatives
Ready to prove it?
Pass the test and this pattern moves unknown
