Here are some simple examples to help you understand recursive procedures and their implementation. This knowledge is crucial when dealing with recursive data structures such as trees and graphs.
Factorial: Factorial is a common example of recursion. It represents multiplying a positive integer by all positive integers smaller than it. For example, the factorial of 4 is written as 4!, and its calculation is 4 × 3 × 2 × 1 = 24. Here's an example code that implements factorial using recursion:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
result = factorial(4)
print(result) # Output: 24
In the above code, the factorial()
function achieves recursion by calling itself. It returns 1 when n
is 0 or 1. Otherwise, the function calls itself to calculate the factorial of n
.
Fibonacci Sequence: The Fibonacci sequence is another common example of recursion. Each number in the sequence is the sum of the two preceding ones. For example, the sequence starts as 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Here's an example code that implements the Fibonacci sequence using recursion:
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
result = fibonacci(6)
print(result) # Output: 8
In the above code, the fibonacci()
function achieves recursion by calling itself. It returns n
when n
is less than or equal to 1. Otherwise, the function calls itself to calculate the sum of the previous two numbers.
Now, let's discuss the classic example of the Tower of Hanoi. The Tower of Hanoi puzzle involves three rods and a set of different-sized disks. The goal is to move all the disks from one rod to another while following the given rules:
- Only one disk can be moved at a time.
- Each move consists of taking the uppermost disk from one of the stacks and placing it on top of another stack.
- No disk may be placed on top of a smaller disk.
Here's an example implementation of the Tower of Hanoi problem using recursion:
def tower_of_hanoi(n, source, destination, auxiliary):
if n > 0:
# Move n-1 disks from the source to auxiliary rod
tower_of_hanoi(n - 1, source, auxiliary, destination)
# Move the nth disk from the source to the destination rod
print("Move disk", n, "from", source, "to", destination)
# Move the n-1 disks from auxiliary to destination rod
tower_of_hanoi(n - 1, auxiliary, destination, source)
# Testing the Tower of Hanoi function
tower_of_hanoi(3, 'A', 'C', 'B')
In the above code, the tower_of_hanoi()
function recursively solves the Tower of Hanoi puzzle. It moves n-1
disks from the source rod to the auxiliary rod, then moves the nth
disk from the source rod to the destination rod, and finally moves the n-1
disks from the auxiliary rod to the destination rod.
These examples illustrate the basic concepts of recursion. Recursive procedures break down a problem into smaller subproblems by calling themselves, and they terminate when the problem size becomes small enough to be directly solved. Recursion is particularly useful when dealing with data structures that exhibit recursive patterns, such as trees and graphs.