Nov 15: Recursion
POGIL Activity¶
- Recursive Functions
- If you are absent today, complete this activity at home
Model 1¶
def factorial(n):
# base case
if n == 0:
return 1
# general case
product = 1
for i in range(n, 0, -1):
product *= i
return product
Model 2¶
def fibonacci(n):
# base case
if n == 1 or n == 2:
return 1
# general case
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == "__main__":
for i in range(1, 6):
print(fibonacci(i))