Solving the Towers of Hanoi by Counting in Binary

Binary carries select the disk to move, and a recursive argument explains why the solution uses the fewest possible moves.

KSKeith SchwarzGast
Kanal
3Blue1Brown
Veröffentlicht
25. Nov. 2016
Video
13:58
Lesezeit
3 Min.
Folien
4
Original
Englisch
Auf YouTube öffnen
Beim Scrollen schrumpft das Video in die rechte untere Ecke und läuft weiter. Untertitel folgen Ihrer Lesesprache.
LesespracheIn 2 Sprachen verfügbar
Diesen Artikel gibt es noch nicht auf Deutsch, daher wird das Original (Englisch) angezeigt.
ZUSAMMENFASSUNG

In 30 Sekunden

3Blue1Brown and Keith Schwarz connect binary counting to the Towers of Hanoi. They show the move rule, explain its recursive structure and derive the minimum of 2ⁿ−1 moves for n disks; a later video covers the connection to Sierpiński’s triangle.

  1. 1Three pegs and disksMove one disk at a time and never place a larger disk on a smaller one.
  2. 2Binary carriesBits represent powers of two and roll over as counting advances.
  3. 3A counting ruleThe position reached by a carry determines which disk moves.
  4. 4The recursive moveMove the smaller stack away, move the large disk, then replace the stack.
  5. 5The minimumThe recurrence T(n)=2T(n−1)+1 yields 2ⁿ−1 moves.

Das Wichtigste

  1. A binary increment selects a disk according to the position reached by its carry.
  2. The smallest disk cycles through the pegs, and each larger disk has a forced legal move.
  3. Moving a large disk requires moving the smaller stack away and then back.
  4. The three-peg puzzle needs at least 2ⁿ−1 moves for n disks, and the recursive solution achieves that bound.
  5. Binary counting and the Hanoi solution share the same recursive structure.
VOLLTEXT

Den bearbeiteten Text nach Kapiteln lesen

Nur das Transkript
01

The Hanoi puzzle

The presenters define the three-peg puzzle and its legal moves.

The Towers of Hanoi can be solved by following the rhythm of binary counting. In this first part, the presenter and former computer-science lecturer Keith Schwarz explain the puzzle and the recursive structure behind the connection; a follow-on video explores Sierpiński’s triangle.

The puzzle has three pegs and disks of different sizes stacked largest to smallest. The aim is to move the whole stack to another peg, moving one disk at a time and never placing a larger disk on a smaller one.

The initial five-disk tower
FOLIE 1 / 4The initial five-disk towerDisks are numbered from smallest to largest, starting at zero.

The example labels the smallest disk 0, then 1, 2 and so on. The first move must be disk 0. Once it has moved, disk 1 cannot go on top of it, so its destination is the remaining peg.

02

The rhythm of binary

Counting in base two reveals a self-similar pattern of carries.

Schwarz’s observation is that the pattern of binary counting determines which disk to move. To see why, first look at the rhythm of carrying in an ordinary positional number system.

In base ten we cycle through digits 0 to 9, then reset the ones place and carry into the tens place. After enough repetitions, the tens place also rolls over and the hundreds place advances. The same pattern repeats at larger scales.

Binary uses only 0 and 1. After 0 and 1, the next number is written 10: one group of two and zero ones. Then 11 represents three; adding one again yields 100, or one group of four.

Binary place values
FOLIE 2 / 4Binary place valuesThe binary number 10 represents one group of two and zero ones.

Binary place values are powers of two: ones, twos, fours, eights and so forth. Carries happen frequently, making the repeated structure especially visible.

At each scale, binary counting has the same shape: count through a smaller range, carry into the next place, then repeat that smaller pattern. For instance, reaching 1111 involves counting the last three bits up, carrying into the eights place and counting them up again.

03

Turn binary carries into moves

Each carry selects a disk, while the smallest disk cycles through the pegs.

Begin counting in binary from zero. Whenever the increment flips only the final bit, move the smallest disk one peg to the right, wrapping from the rightmost peg back to the first. This fixed direction determines which of the other pegs receives the finished tower; reversing the direction selects the other destination.

The smallest disk and the last bit
FOLIE 3 / 4The smallest disk and the last bitFlipping only the last bit corresponds to moving disk 0.

When a carry reaches the twos place, move disk 1. It has only one legal destination other than the peg holding disk 0. A carry to the fours place calls for disk 2, a carry to the eights place calls for disk 3, and so on.

The resulting sequence starts 0, 1, 0, 2, 0, 1, 0, 3. The rule looks surprising: why is the indicated larger disk free whenever the corresponding carry occurs, and why does the process finish efficiently?

04

Why recursion gives the minimum

Moving the largest disk requires solving the smaller problem before and after it.

Schwarz explains the recursive viewpoint. To move a large disk to its destination, all smaller disks must first be stacked on the spare peg. Move the large disk once, then move that smaller stack onto it.

Move the smaller stack first
FOLIE 4 / 4Move the smaller stack firstThe larger disk can move only after all smaller disks occupy the spare peg.

The smaller stack is the same kind of problem on fewer disks. Each disk follows the same pattern: move the disks above it away, move the disk, then put the smaller stack back on top.

The reasoning also establishes optimality. Before the largest disk can move, every smaller disk must be moved away; afterward, every smaller disk must be moved onto it. If T(n) is the minimum number of moves, T(n)=2T(n−1)+1 with T(0)=0, giving T(n)=2ⁿ−1.

05

Why the patterns match

Binary increments and the recursive solution share the same structure.

Binary counting mirrors this recursion: complete the smaller pattern, carry into the next bit, and complete the smaller pattern again. The disk moved at each step corresponds to the highest bit changed from 0 to 1 by the increment.

For two disks, the sequence is disk 0, disk 1, disk 0, matching the pattern of binary increments up to 11. For three disks, solve the two-disk problem, move disk 2 and solve the two-disk problem again, matching the larger binary pattern.

At every scale, both processes have the structure “subproblem, one step, subproblem.” Looking at the bits that change therefore lets us reconstruct the recursive Hanoi algorithm. The promised connection to Sierpiński’s triangle is left for part two.

Mehr aus diesem Kanal

20 Folien40:44EN
3Blue1Brown· 16. Okt. 2026Grant Sanderson: There's never been a better time to go into mathGrant Sanderson, Constantin Kogler · 3Blue1BrownStudents fear AI has solved mathematics. Grant Sanderson argues the opposite: even if machines prove theorems better than we do, the institution of math stays valuable—and this is a rare moment when newcomers can reshape it.MathematicsAI for scienceHuman-AI Collaboration+2
29 Folien51:55EN
3Blue1Brown· 18. Sept. 2026The last IMO problem AI could not solveGrant Sanderson · 3Blue1BrownGrant Sanderson walks through IMO 2025 Problem 6 — a tiling puzzle that frontier models missed — and the visual patience it takes to prove a construction is optimal.MathematicsAI researchReasoning & test-time compute+2
0:55EN
3Blue1Brown· 24. Juli 2026The 64 Sugar Cubes Puzzle: Six Equal DistancesA 4×4×4 block has three colors; prove that six same-colored pairs share a distance.MathematicsEducation
33:51EN
3Blue1Brown· 16. Juli 2026What Cross-Entropy Connects: Compression, Language Models and KL DivergenceGrant Sanderson · 3Blue1BrownGrant Sanderson builds cross-entropy from code lengths and uses it to explain next-token training and distillation.MathematicsMachine LearningFoundation Models+1
Folien2:25EN
3Blue1Brown· 12. Juni 2026How Shannon Measured the Entropy of EnglishA guessing game helped Claude Shannon estimate how much information English text contains, and why prediction is central to compression.MathematicsAI History
Folien1:02EN
3Blue1Brown· 10. Juni 2026A Robot Walk and the Puzzle of Optimal Binary EncodingHow few bits does it take to send directions that occur with unequal frequencies?MathematicsScience Communication