Recursion
Unlock the power of functions that call themselves. Master base cases, recursive calls, and classic recursive algorithms.
Learning Objectives:
- β’Understand the two parts of every recursive function (base case + recursive call)
- β’Implement recursive sum, power, and factorial
- β’Compute Fibonacci numbers recursively
- β’Calculate the sum of digits recursively
- β’Solve the Tower of Hanoi
What is Recursion?
A recursive function is one that calls itself. Every recursive function has two essential parts:
- Base case β the condition where the function stops calling itself
- Recursive call β the function calling itself with a simpler input
Without a base case, the function would call itself forever (stack overflow!).
A Simple Example: Counting Down
pythondef countdown(n): if n == 0: # base case print("Go!") else: print(n) countdown(n - 1) # recursive call with n-1 countdown(3) # 3 # 2 # 1 # Go!
Recursive Sum
Sum all integers from 1 to N:
plaintextsum(5) = 5 + sum(4) = 5 + 4 + sum(3) = 5 + 4 + 3 + sum(2) = 5 + 4 + 3 + 2 + sum(1) = 5 + 4 + 3 + 2 + 1 + sum(0) = 5 + 4 + 3 + 2 + 1 + 0 = 15
pythondef sum_to_n(n): if n == 0: # base case return 0 return n + sum_to_n(n - 1) # recursive call
Recursive Power
Compute x^n:
plaintextpower(2, 4) = 2 * power(2, 3) = 2 * 2 * power(2, 2) = 2 * 2 * 2 * power(2, 1) = 2 * 2 * 2 * 2 * power(2, 0) = 2 * 2 * 2 * 2 * 1 = 16
pythondef power(x, n): if n == 0: # base case: anything^0 = 1 return 1 return x * power(x, n - 1)
The Call Stack
Each recursive call adds a frame to the call stack. When the base case is reached, the stack unwinds β each frame returns its value to the caller.
plaintextpower(2, 3) ββ 2 * power(2, 2) ββ 2 * power(2, 1) ββ 2 * power(2, 0) ββ 1 β base case ββ 2 * 1 = 2 β unwind ββ 2 * 2 = 4 ββ 2 * 4 = 8
The Tower of Hanoi
The classic puzzle: move N disks from peg A to peg C using peg B as auxiliary, never placing a larger disk on a smaller one.
The recursive insight:
- Move the top N-1 disks from A to B (using C)
- Move disk N from A to C
- Move the N-1 disks from B to C (using A)
pythondef hanoi(n, source, target, auxiliary): if n == 1: print(f"Move disk 1 from {source} to {target}") return hanoi(n - 1, source, auxiliary, target) print(f"Move disk {n} from {source} to {target}") hanoi(n - 1, auxiliary, target, source)
For 3 disks, this produces 7 moves (2^n - 1 moves in general).
Watch Out for Stack Depth!
Python's default recursion limit is 1000. For very deep recursion (large N), an iterative approach is safer. For our exercises, inputs are small enough that recursion is fine.
Exercises
Exercise 1: Recursive Sum
Use recursion to calculate the sum from 1 through N.
Exercise 2: Recursive Power
Use a recursive function to calculate x raised to the power n.
Exercise 3: Recursive Digit Sum
Use recursion to calculate the sum of the digits of a positive integer.
Exercise 4: Recursive Fibonacci
Use recursion to calculate the Nth Fibonacci number.
Exercise 5: Recursive Greatest Common Divisor
Use the recursive Euclidean algorithm to calculate the greatest common divisor.
Exercise 6: Tower of Hanoi
Solve the Tower of Hanoi by moving the disks from peg A to peg C.