Tower of Hanoi
Tap a peg to lift its top disc
Tap a peg to lift its top disc
All the discs start stacked on the left peg, largest at the bottom. Move them all to another peg. You may move one disc at a time, only the top disc of a peg, and you may never place a larger disc on top of a smaller one. Tap a peg to lift its top disc, then tap the peg you want it on. With n discs the puzzle can always be solved, and the shortest possible solution takes exactly two to the power n, minus one moves.
The Tower of Hanoi was introduced by the French mathematician Edouard Lucas in 1883, along with a story about monks moving 64 golden discs, the world ending when they finish. The story is invented but the maths is real. Moving n discs takes a minimum of two to the power n, minus one, moves: 7 for three discs, 15 for four, 31 for five, and so on. For 64 discs that is over eighteen quintillion moves, which at one move per second is longer than the current age of the universe.
The puzzle is a favourite teaching example for recursion because the solution is so cleanly recursive: to move n discs, move n minus one out of the way, move the biggest, then move the n minus one back on top.
Two to the power of the number of discs, minus one. Three discs need 7, four need 15, five need 31, seven need 127.
The smallest disc moves every other turn, always in the same direction, and the remaining move is always forced. Follow that and you hit the minimum.
With an odd number of discs the first move goes to the target peg; with an even number, to the spare. Get it wrong and the minimum becomes impossible.
-FAQ