Factorial Expression 2: - Factorial Expression 2:
- fact(n) = n * (n – 1) * (n – 2) * … * 1
- fact(n – 1) = (n – 1) * (n – 2) * … * 1
- fact(n – 2) = (n – 2) * … * 1
- :
- fact(1) = 1
- I.e.,
- fact(n) = n * (n – 1) * (n – 2) * … * 1
-
- = n * fact(n – 1)
- Suppose that the general form of the problem has some parameter n (integer)
- Simply assume that the answer for the case n-1 is known (given)
- Then express the solution for the n case in terms of the n-1 case
- Find “base case”
Do'stlaringiz bilan baham: |