Skip to content

Solve

Tower of Hanoi Solver

The shortest solution to any legal position — not just a full stack. Set the board up as it actually is, and watch the optimal path play out.

Optimal solution

31moves25 − 1 = 31

The classic puzzle: every disk starts on tower A and finishes on tower C.

Ready to playStep 0 of 31

stepSpaceplayHomeEndjump

Every move, in order

Disk 1 is the smallest, and A → C means: lift the top disk off tower A and place it on tower C. Select any row to send the board to that move.

Move 16 is disk 5 crossing from A to C. The 15 moves either side of it are the same solution twice: the 4-disk solution, played first onto the spare tower and then off it.

#DiskFromTo
11AC
22AB
31CB
43AC
51BA
62BC
71AC
84AB
91CB
102CA
111BA
123CB
131AC
142AB
151CB
165AC
171BA
182BC
191AC
203BA
211CB
222CA
231BA
244BC
251AC
262AB
271CB
283AC
291BA
302BC
311AC
On this page
  1. The solver
  2. How the solver works
  3. Reading a solution
  4. Taking a solution with you
  5. Why the list stops at 12 disks
  6. Frequently asked questions

How the solver works

The method is the puzzle's own logic, run forwards. To seat the largest disk on its target tower, every smaller disk has to be somewhere else — specifically, all of them stacked on the one remaining tower. So the solver asks for that smaller stack to be moved there first, moves the large disk across, then asks for the smaller stack to be brought over on top of it. Each of those requests is the same problem with one fewer disk, all the way down to a single disk, which moves in one step.

What makes this solver different from the ones that only handle a full stack is one extra line of reasoning: if the largest disk is already on its target, there is nothing to do for it, and the whole problem shrinks to the disks above it. That single case is what lets it solve from a board you are half way through rather than making you start again — and it is why the move counts it reports from a custom position are smaller than 2ⁿ − 1, often far smaller for a game you were part-way through.

  1. Move the n−1 disks above it onto the spare tower. Nothing else can happen until they are out of the way.

  2. The largest disk is free now, and its target is empty. It crosses in a single move — the only move this phase needs.

  3. Rebuild the n−1 stack on top of it. That is the same problem again with one fewer disk, all the way down to a single disk that moves in one step.

Solving n disks in three phases. The shaded disks are the ones that move; the spare tower is whichever of the three is neither the source nor the target, and it changes at every level of the recursion.

Reading a solution

Each row names one move: which disk, which tower it leaves, which tower it lands on. Disk 1 is the smallest. You never need to check legality — every move in the list already is legal, by construction.

From a full stack, two patterns show up immediately:

  • The smallest disk moves on every other turn — moves 1, 3, 5 and so on — and always travels the same way round the three towers.
  • Every move in between is the only legal move that does not touch the smallest disk.

That is the whole iterative method, and you can read it straight off any full-stack solution here. From a custom position the list is still the shortest, but it is stitched together from smaller transfers, so the smallest disk can change direction partway through. The iterative solution shows why the method works, and the strategies guide turns it into something to play by. For the recursion itself, traced line by line in six languages, see the Tower of Hanoi algorithm.

The common sizes also have a page each, with every move listed and what is particular about that size: solutions from 3 to 10 disks, and the 64 disks of the legend.

Taking a solution with you

The address bar always describes the board on screen. A full transfer reads ?disks=5&from=A&to=C; a custom position adds a code like pos=CBBAA, one letter per disk with the smallest first. Copy that link and it reopens on exactly this puzzle — which is also how you send someone the position you are stuck in rather than a description of it.

The move list itself copies to the clipboard, or downloads as a plain text file to read beside a physical puzzle and as a CSV to open in a spreadsheet. The download is not capped the way the printed table is: at fifteen disks the file still holds all 32,767 moves.

Why the list stops at 12 disks

Solutions double with every disk. Twelve disks is 4,095 moves, which is a long but usable table. Thirteen is 8,191, twenty is 1,048,575, and no browser renders a million-row table into anything a person can use. Above twelve disks this page gives the exact count and keeps the animated board — and the downloads above still contain every move, so the solution is still yours to take away; it simply is not printed. For counts alone, the calculator goes to sixty-four disks and beyond.

Frequently asked questions

What does the Tower of Hanoi solver do?

It prints the shortest possible sequence of moves that gathers every disk onto one tower. Give it a number of disks and it solves the classic full transfer; give it a partly-finished position and it solves from there, which is the part most solvers cannot do.

Is the solution the shortest one?

Yes. The solver is provably optimal, not merely correct: from a full starting stack it produces exactly 2ⁿ − 1 moves, and from any other legal position it produces the true shortest path to the goal. There is no sequence with fewer moves.

Can it solve from a position I have already started?

Yes — that is what the custom start position is for. Put each disk on the tower it is currently on and the solver will finish the job in the fewest moves available from there, which is usually far fewer than starting over.

Can I share or bookmark a solution?

Yes. The address bar always holds the board you are looking at: /solver/?disks=5&from=A&to=C is a full five-disk transfer, and a custom position adds a code such as pos=CBBAA — one letter per disk, smallest first. Copy the link and it reopens on exactly that puzzle.

Can I download the move list?

Yes, as a plain .txt file or as a .csv with one row per move, alongside the copy button. The download works even for the very long solutions the page will not print as a table, so a 15-disk answer is still yours to keep.

How do I read the move list?

Each line names one move: the disk number, the tower it leaves, and the tower it lands on. Disk 1 is the smallest. Play them in order and the puzzle solves itself; you never have to think about which disk is legal, because every move listed already is.

Why does the move list stop at 12 disks?

A 12-disk solution is 4,095 moves. At 20 disks it would be 1,048,575 rows, which no browser will render usefully — so above 12 disks the page gives the exact move count and the formula instead of the list, and the animated board still plays the solution.