













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
1 / 53
This page cannot be seen from the preview
Don't miss anything!














































IN ORDER TO UNDERSTAND RECURSION, ONE SHOULD FIRST UNDERSTAND RECURSION.
Every recursive function definition includes two parts:
4! = 4(3!) 3! = 3(2!) 2! = 2(1!) 1! = 1 (0!) = 1(1) = 1 Compute the base case make smaller instances of the same problem
4! = 4(3!) 3! = 3(2!) 2! = 2(1!) = 2 1! = 1 (0!) = 1(1) = 1 Compute the base case make smaller instances of the same problem build up the result
4! = 4(3!) = 24 3! = 3(2!) = 6 2! = 2(1!) = 2 1! = 1 (0!) = 1(1) = 1 Compute the base case make smaller instances of the same problem build up the result
def factorial(n) : if n == 0: # base case return 1 else: # recursive case return n * factorial(n-1)
factorial(4)? = 4 * factorial(3) S T A C K n=
factorial(4)? = 4 * factorial(3) S T A C K factorial(3)? n= n=
factorial(4)? = 4 * factorial(3) S T A C K factorial(3)? = 3 * factorial(2) factorial(2)? n= n= n=
factorial(4)? = 4 * factorial(3) S T A C K factorial(3)? = 3 * factorial(2) factorial(2)? = 2 * factorial(1) n= n= n=