In this lesson, you'll explore recursive algorithms and functions, a key concept in advanced algorithms. Recursion allows functions to solve problems by breaking them into smaller, similar subproblems.
By the end of this lesson, you will be able to:
Recursion might seem complex at first, but it’s a clever way for a function to tackle a problem by calling itself with a simpler version of that problem. Picture yourself organizing a stack of nested boxes: you open one box, then deal with a smaller set of boxes inside it, repeating the process until you find an empty box. That’s recursion.
Every recursive function has two main parts:
When a function calls itself, it uses something called the 'call stack' – think of it as a stack of plates. Each call adds a plate to the stack. When it reaches the base case, it starts removing plates (returning values) from the bottom up, building the final answer.
Let's take a look at the key differences between iterative and recursive approaches, building on the examples you've seen in this lesson, like factorial and Fibonacci.
Recursive Approach: A recursive function solves a problem by calling itself with a smaller version of the same problem. It relies on a base case to stop the recursion and recursive cases to break down the problem. For example, in the recursive factorial, each call computes n * factorial(n-1) until reaching the base case (n=0 or 1). This uses the call stack, which can lead to stack overflow for large inputs, and may be less efficient due to repeated calculations.
Iterative Approach: An iterative function uses loops (like for or while) to repeat steps without calling itself. It builds the solution step by step, often using variables to track progress. For instance, the iterative factorial multiplies numbers in a loop from 1 to n, using constant space (O(1)) and linear time (O(n)). It's generally more efficient, avoids recursion depth issues, and is better for large inputs.
Key Differences:
In VS Code, create a new folder called RecursionLesson and a file named recursion.py inside it.
Calculating the factorial of a number involves multiplying the number by the factorial of the number one less than it. For example, the factorial of 5 (written 5!) is 5 * 4 * 3 * 2 * 1 = 120. Recursion works here because each factorial can be broken down into a smaller factorial problem.
We'll write a recursive function to compute the factorial of a number, where the base case is 0! or 1! (both equal 1), and the recursive case multiplies the number by the factorial of the number minus one.
Add the following complete code to your file:
def factorial(n):
# Base case: if n is 0 or 1, the factorial is 1
if n == 0 or n == 1:
return 1
# Recursive case: calculate factorial by multiplying n with factorial of (n-1)
previous_factorial = factorial(n - 1)
current_factorial = n * previous_factorial
return current_factorial
# Test the function
print("Factorial of 5:", factorial(5)) # Output: 120
print("Factorial of 3:", factorial(3)) # Output: 6
print("Factorial of 0:", factorial(0)) # Output: 1
Save and run the file in VS Code terminal with python recursion.py. You should see 120, 6, and 1 printed.
Try testing with other inputs like print(factorial(4)) (should print 24) or print(factorial(1)) (should print 1) to verify.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.