Problem. Move disks from peg A to peg C using peg B, with two rules: move one disk at a time, and never place a larger disk on a smaller one.

The recursive solution

void hanoi(int n, char from, char to, char via) {
    if (n == 0) return;
    hanoi(n - 1, from, via, to);
    printf("move disk %d: %c -> %c\n", n, from, to);
    hanoi(n - 1, via, to, from);
}

Move the top aside, move the largest, move the back.

and this is optimal — the largest disk must move at least once, and before it can, all others must be on the spare peg.

The iterative solution

The move sequence has a beautiful closed form:

On move (1-indexed), move disk — the number of trailing zeros of .

for (long long i = 1; i <= (1LL << n) - 1; i++) {
    int disk = __builtin_ctzll(i);
    // the disk moves in a fixed cyclic direction depending on parity of n and disk
}

Disk 0 (the smallest) moves on every odd step; disk 1 on every 4th; disk on every -th. The smallest disk always cycles in one direction ( if is odd, the other way if even), and on the other moves there is only ever one legal move not involving it.

This is exactly a Gray code: the bit that changes from to is , and each disk’s position corresponds to a bit.

The state graph

The legal configurations form a graph whose structure is the Sierpiński triangle. The optimal solution is a path along one edge of it, and the graph’s diameter is .

Consequences:

  • The shortest path between any two configurations is computable in by a greedy recursion.
  • Moving from an arbitrary configuration to another is not always — that is only the corner-to-corner distance.

Variants

VariantResult
disks, 3 pegs moves
4 pegs (Reve’s puzzle)Frame-Stewart: ; proven optimal in 2014
pegsFrame-Stewart generalises; optimality open for
Cyclic Hanoi (moves only ) moves
Bicolour / two-stack variantsdifferent recurrences
Arbitrary start and end configuration greedy
Count moves of a specific disk
Which peg is disk on after moves?read the bits of

Frame-Stewart for 4 pegs: move disks to a spare peg (using all 4), move the remaining using 3 pegs ( moves), then move the back. Choosing the optimal gives a DP.

Why it is worth knowing

The problem is the canonical illustration of three ideas:

  1. Recursion with a self-similar structure — the classic first example of “solve two smaller copies”.
  2. A lower bound proved by necessity, not by exhaustion.
  3. The recursive and iterative forms of the same sequence — one derived from the recursion, the other from the binary representation. Seeing that they agree is a good exercise in the connection between recursion and bit patterns.

The ctz fact generalises: whenever a recursive process splits as and produces a sequence, that sequence usually has a description in terms of the binary expansion of the step index.

The legend

The Tower of Brahma has 64 golden disks. At one move per second, moves take about 585 billion years — around 42 times the current age of the universe.

See also: Gray Code · Divide and Conquer · Classical Problems