1. Factorial
Factorial¶
In [2]:
Copied!
def factorial(n):
# 1. Define the base case
if n == 0:
return 1
# 2. Define the recurrence relation
# solve the samller problem first
smaller_problem = factorial(n - 1)
bigger_problem = n * smaller_problem
return bigger_problem
def factorial(n):
# 1. Define the base case
if n == 0:
return 1
# 2. Define the recurrence relation
# solve the samller problem first
smaller_problem = factorial(n - 1)
bigger_problem = n * smaller_problem
return bigger_problem
In [3]:
Copied!
factorial(4)
factorial(4)
Out[3]:
24
In [ ]:
Copied!