GenerateRandomSearch

Tower of Hanoi Solver

Move a stack of discs to another peg, one at a time, never placing a larger disc on a smaller one. The puzzle is famous for its recursion — to move n discs you must first move n−1 out of the way — and for the consequence of that recursion: the number of moves doubles with every disc you add, so the minimum is exactly two to the power n, minus one. That is a theorem, not an estimate, which makes it something a page can check itself against.

What this generator does

Produces the full optimal move sequence by the standard recursion, then replays every move against three pegs to confirm each is legal — the right disc is on top, and it never lands on a smaller one — and that the stack genuinely finishes on the last peg. The move count is checked against the two-to-the-n bound separately.

How to use this tool

  1. Choose how many discs and generate the solution.
  2. Read the move count and confirm it matches two to the power of the disc count, minus one.
  3. Show every step to follow the solution move by move.
  4. Add one disc and watch the move count double exactly.

Understanding the controls

How many discs
Between 3 and 10. Ten discs need 1,023 moves, which is about as long a list as is worth reading on a page.
Show every step
Lists all the moves in order, each naming the disc and the pegs it travels between.

Common use cases

  • Teaching recursion with a problem whose cost is provably exponential
  • A worked move list for demonstrating the puzzle physically
  • Showing why doubling the input can double the work forever
  • Test data for a Hanoi implementation
  • Producing a printable step list for a classroom activity

How this generator works

The recursion is the whole solution: to move n discs from one peg to another, move n−1 to the spare peg, move the largest across, then move the n−1 back on top. Every step is generated by that rule and then replayed against a simulated set of pegs, which is a genuinely independent check — the replay knows the rules of the puzzle but nothing about how the moves were chosen.

Randomness and fairness

There is no randomness here at all. The optimal solution to the Tower of Hanoi is unique up to which peg you finish on, so the same disc count always produces the same move list.

For how randomness is produced across the whole site, see how Generate Random works.

Limitations and good to know

  • Three pegs only. With four or more the shortest solution is a different and much harder problem, still unproven in general.
  • Up to ten discs, above which the move list becomes too long to display usefully.
  • The solution always moves the stack to the third peg.
  • No interactive board is offered; this produces the move list rather than playing the puzzle.
  • Solutions are not stored between visits.

Privacy and your data

The move list is generated in your browser and nothing about it is transmitted, logged or stored.