Tower of Hanoi — Definition, Formula & Examples
The Tower of Hanoi is a mathematical puzzle where you move a stack of differently sized disks from one peg to another, one disk at a time, without ever placing a larger disk on top of a smaller one.
The Tower of Hanoi is a problem defined on three pegs and ordered disks of distinct sizes. The objective is to transfer all disks from a source peg to a target peg, moving exactly one disk per step, subject to the constraint that no disk may rest on a smaller disk. The minimum number of moves required is .
Key Formula
Where:
- = Minimum number of moves to solve the puzzle with n disks
- = Number of disks
How It Works
The puzzle is solved recursively. To move disks from peg A to peg C, first move the top disks from A to the spare peg B. Then move the largest disk directly from A to C. Finally, move the disks from B to C. Each subproblem has the same structure, which is why this puzzle is a classic example of recursion. The base case is : simply move the single disk to the target peg.
Worked Example
Problem: Find the minimum number of moves needed to solve the Tower of Hanoi with 4 disks.
Apply the formula: Substitute into the minimum-moves formula.
Compute: Calculate the power of 2 and subtract 1.
Answer: The minimum number of moves to solve the Tower of Hanoi with 4 disks is 15.
Visualization
Why It Matters
The Tower of Hanoi is one of the first problems students encounter when learning recursion in computer science courses. Its exponential growth—doubling with each added disk—provides a concrete, tangible example of time complexity. It also appears in algorithm design textbooks as a gateway to divide-and-conquer strategies.
Common Mistakes
Mistake: Confusing the recursive formula with the closed-form and miscounting moves.
Correction: Both are correct representations. The recursive relation describes the strategy (move disks, move the largest, move again), while is its closed-form solution. Use whichever fits the context, but verify they agree.
