The Tower of Hanoi problem involves moving a tower of n
disks from the source peg to the destination peg, using an auxiliary peg. The rules of the problem are:
- Only one disk can be moved at a time.
- A larger disk cannot be placed on top of a smaller disk.
- The tower of disks must be moved from the source peg to the destination peg.
Initially, all 3 disks are on the source peg (A), with the largest disk at the bottom and the smallest disk at the top. The goal is to move the entire tower to the destination peg (C), using the auxiliary peg (B) as needed.
- 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
After these 7 moves, the entire tower of disks has been successfully moved from the source peg to the destination peg, following the rules.
The minimum number of moves required to solve the Tower of Hanoi problem with n
disks is calculated as:
[ \text{Moves} = 2^n - 1 ]
For example, for 3 disks: [ \text{Moves} = 2^3 - 1 = 7 ]
This repository contains code to solve the Tower of Hanoi problem programmatically. The implementation uses recursion to model the step-by-step solution.
- Supports any number of disks.
- Visualizes the sequence of moves.
Tower of Hanoi.py
: Python implementation of the Tower of Hanoi algorithm.README.md
: Explanation and usage details.
- Clone this repository.
- Run the
Tower of Hanoi.py
file using Python. - Input the number of disks when prompted.
This project is licensed under the MIT License. See the LICENSE
file for details.
Happy Coding!