Algorithms · interactive · step by step

Tower of Hanoi

Watch the recursive solution move by move, with the recursion tree and call stack, or play it yourself with hints. From 1 to 12 discs.

Step 0Space play · ←→ step

Worked example

The 7 moves for 3 discs

MoveDiscFrom → to
11A → C
22A → B
31C → B
43A → C
51B → A
62B → C
71A → C

Code

Python
def hanoi(n, source, target, spare):
    if n == 0:
        return
    hanoi(n - 1, source, spare, target)
    print(f"Move disc {n} from {source} to {target}")
    hanoi(n - 1, spare, target, source)

hanoi(3, "A", "C", "B")  # 7 moves
JavaScript
function hanoi(n, source, target, spare) {
  if (n === 0) return;
  hanoi(n - 1, source, spare, target);
  console.log(`Move disc ${n} from ${source} to ${target}`);
  hanoi(n - 1, spare, target, source);
}
hanoi(3, 'A', 'C', 'B'); // 7 moves
C++
#include <iostream>
void hanoi(int n, char source, char target, char spare) {
    if (n == 0) return;
    hanoi(n - 1, source, spare, target);
    std::cout << "Move disc " << n << " from " << source << " to " << target << "\n";
    hanoi(n - 1, spare, target, source);
}
int main() { hanoi(3, 'A', 'C', 'B'); }
Java
static void hanoi(int n, char source, char target, char spare) {
    if (n == 0) return;
    hanoi(n - 1, source, spare, target);
    System.out.println("Move disc " + n + " from " + source + " to " + target);
    hanoi(n - 1, spare, target, source);
}

Questions

Tower of Hanoi FAQ

Free, private and works offline: everything runs in your browser.

How do you solve the Tower of Hanoi?

To move n discs from A to C: move the top n − 1 discs to the spare peg B, move the largest disc to C, then move the n − 1 discs from B onto it. Each “move n − 1 discs” is the same puzzle again, one disc smaller, so the solution is recursive. Press Play to watch it, or switch to “Play it yourself”.

What is the minimum number of moves?

2ⁿ − 1 for n discs: 1 → 1, 2 → 3, 3 → 7, 4 → 15, 5 → 31, 6 → 63, 7 → 127, 8 → 255, 10 → 1023. Each extra disc doubles the work plus one, because the n − 1 smaller discs have to be moved twice.

What are the rules?

Move one disc at a time, only the top disc of a peg can move, and a disc may never sit on a smaller disc. The goal is to move the whole stack to another peg.

What is the time complexity of the recursive algorithm?

O(2ⁿ) moves and O(n) memory for the recursion stack. The recurrence is T(n) = 2T(n − 1) + 1, which solves to 2ⁿ − 1. With 64 discs (the legend) it would take 18,446,744,073,709,551,615 moves.

How does the recursion tree help?

Every call hanoi(n, from → to) makes two smaller calls and one move. The tree shows all calls; the highlighted path is the call stack at the current move, which is exactly what the computer keeps in memory while recursing.

Is there a pattern for the moves?

Yes. The smallest disc moves every other turn, always in the same direction (A → C → B → A for an odd number of discs, A → B → C → A for even). In between there is only one legal move that doesn’t use the smallest disc.

Built and maintained by Zubaer Ahammed · Updated · Runs on your device; nothing you add is uploaded