Tower of Hanoi
Move the whole stack to another peg, bigger disks never on smaller ones, with 3 to 8 disks, an optimal move benchmark and an auto solver.
FAQ
How many moves does it take at minimum?
What is the idea behind the optimal solution?
How does the auto solver work?
Where are best scores kept?
How to use
- Know the goal
Carry the whole stack from the left peg to the right, one top disk at a time, and never rest a bigger disk on a smaller one.
- Pick and drop
Click a peg to lift its top disk, click the target peg to drop it, click the same peg to cancel, and invalid moves explain themselves.
- Watch the benchmark
The optimal count for n disks is two to the power of n minus one, always visible, and the win banner reports how close you landed.
- Use the auto solver
When stuck, press auto solve and the recursive solver plays the perfect line step by step, ready for you to replay by hand.
About this tool
The Tower of Hanoi is the classic recursion puzzle: three pegs, a stack of disks of different sizes, and the task of moving the whole stack from the left peg to the right, one top disk at a time, with a bigger disk never resting on a smaller one. Six difficulty levels from 3 to 8 disks are available, the optimal move benchmark is always on screen, and the win banner tells you exactly how your run compared.
A built-in auto solver generates the complete move sequence with recursion and plays it back step by step, which is the fastest cure for being stuck. Best results are stored per disk count, encouraging you to clear first, trim moves second, and finally hit the optimal line in one run. Eight disks take 255 moves, a true test of patience and planning. Watching the solver first and replaying by hand builds intuition faster than grinding alone.
How it works
The optimal solution is recursion in its purest form: moving n disks from start to target equals moving the top n minus one to the spare, moving the biggest disk, then moving that stack back on top. Each of the three steps is the same problem in smaller form, and unrolling gives exactly two to the power n minus one moves. The bound is tight because the biggest disk needs its single move, and nothing fewer can succeed.
The solver builds the whole queue offline and replays it, and the queue length is by construction the optimal count. In manual mode the page tracks selection and legality, rejecting empty picks and bigger on smaller moves explicitly rather than silently. Since each extra disk doubles the optimal count, the 255 moves of the 8 disk tower demand a stable plan, which is why this century old puzzle still teaches recursion better than any lecture.
When to use it
- Teaching recursion to kids
Clear 3 and 4 disks first, then ask how many moves 5 will take and let them discover the doubling. Turning abstract recursion into hand movements is what makes this toy a classic teaching aid. Asking why after each move turns a toy into a genuine lesson.
- A thinking break on commutes
Four to six disks make a session with continuous small decisions and no drag. Clearing one and comparing with the optimum feels far better than idling in a feed. Comparing with the optimum at the end of each run makes progress visible.
- Warming up before interviews
Hanoi is a frequent recursion question in technical interviews. Clear a few levels by hand, then run the auto solver against your own plan to see every edge case laid bare. Treat the page as an interactive whiteboard for the classic question.
Tips & tricks
- Memorize the core line: to move disk n, first park the n minus one disks above it on the spare peg.
- The opening move differs for odd and even stacks, so decide the first step before touching anything.
- If the move count already exceeds the optimum by a lot, restarting beats wandering.
- For 8 disks, validate your plan on 5 first, then scale up instead of grinding from scratch.
Things to watch
- Moving without a plan
Random early moves drift far off the optimal path and can double your count without progress. Decide when the biggest disk moves and where its stack parks before touching anything. Plan the biggest disk first and let everything else serve that plan.
- Underusing the spare peg
The middle peg is not decoration, since nearly every step hands through it. All three pegs are working pegs, and your plan should always assign roles to each. Ask which peg each move frees up before making it. A good habit is narrating the role of every peg as you move.
- Grinding the big towers blind
Seven disks take 127 moves and eight take 255, far too long for pure trial and error. Watch the demo on high levels to see the structure, then replay it in segments. Watch, then replay in segments, and the tower falls.