Python Program for Tower of Hanoi
Last Updated :
30 Oct, 2025
Given a number of disks n, the task is to move all disks from the source rod to the destination rod following these rules:
- Only one disk can be moved at a time.
- A larger disk cannot be placed on top of a smaller disk.
- Only the top disk of any rod can be moved.
The Tower of Hanoi puzzle illustrates recursion by breaking a big problem into smaller steps. The Carousel below shows how the disks move step-by-step between rods:
Let's explore different methods to solve this problem.
Using Iterative Approach
In this approach, the puzzle is solved iteratively using bitwise operations and stack logic. It eliminates recursion and uses the pattern of moves based on the number of disks. This method is more efficient for larger n as it avoids deep recursion calls.
Python
n = 4
src = 'A'
aux = 'C'
des = 'B'
total_moves = 2 ** n - 1
if n % 2 == 0:
des, aux = aux, des
rods = { 'A': list(range(n, 0, -1)), 'B': [], 'C': [] }
for move in range(1, total_moves + 1):
if move % 3 == 1:
from_rod, to_rod = ('A', 'B') if (rods['A'] and (not rods['B'] or rods['A'][-1] < rods['B'][-1])) else ('B', 'A')
elif move % 3 == 2:
from_rod, to_rod = ('A', 'C') if (rods['A'] and (not rods['C'] or rods['A'][-1] < rods['C'][-1])) else ('C', 'A')
else:
from_rod, to_rod = ('B', 'C') if (rods['B'] and (not rods['C'] or rods['B'][-1] < rods['C'][-1])) else ('C', 'B')
disk = rods[from_rod].pop()
rods[to_rod].append(disk)
print(f"Move disk {disk} from {from_rod} to {to_rod}")
Output
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Move disk 3 from A to B
Move disk 1 from C to A
Move disk 2 from C to B
Move disk 1 from A to B
Move disk 4 from A to C
Move disk 1 from B to C
Move disk 2 from B to A
Move disk 1 from C to A
Move disk 3 from B to C
Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C
Explanation:
- The loop iteratively performs the disk movements following the Tower of Hanoi rules.
- It calculates the total number of moves as 2^n - 1 and determines which disk to move in each step using bitwise operations.
- Instead of recursive calls, the sequence of moves is generated through iteration, efficiently simulating the recursive process.
Using Recursion
In this method, recursion is used to break down the problem into smaller subproblems. The idea is to move n-1 disks to an auxiliary rod, move the largest disk to the destination rod, and then move the n-1 disks from the auxiliary rod to the destination.
Python
n = 4
src = 'A'
des = 'B'
aux = 'C'
def move_disks(n, src, des, aux):
if n == 1:
print(f"Move disk 1 from {src} to {des}")
return
move_disks(n - 1, src, aux, des)
print(f"Move disk {n} from {src} to {des}")
move_disks(n - 1, aux, des, src)
move_disks(n, src, des, aux)
Output
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Move disk 4 from A to B
Move disk 1 from C to B
Move disk 2 from C to A
Move disk 1 from B to A
Move disk 3 from C to B
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Explanation:
- move_disks() recursively divides the problem into smaller steps.
- First, it moves the top n-1 disks to the auxiliary rod then it moves the largest disk to the destination rod.
- Finally, it moves the n-1 disks from the auxiliary rod to the destination and the recursive calls print every step needed to complete the puzzle.
Practice Tower of Hanoi Problem Yourself
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice