Recursion for Factorial
- What is a subproblem of the factorial
problem that is of the same form?
- n! = n × (n-1) × (n-2) × ... 2 × 1
- But (n-1) × (n-2) × ... 2 × 1 = (n-1)!
- So, n! = n × (n-1)!
- Similarly, (n-1)! = (n-1) × (n-2)!
- And so on, till eventually, 1! = 1
- An alternative mathematical definition of
factorial is:
n! | = | 1 if n=1
|
| = | n × (n-1)! otherwise
|
- The Base Case Imperative: In a recursive
procedure, all roads must lead to a base
case.