Chapter 4 · Simulation

Towers of Hanoi Simulator

← Chapter 4
A
B
C
Click a source tower, then a target tower.

Towers of Hanoi Simulator

Move every disk from tower A to tower C. You may move only one disk at a time, and only the top disk of a tower; a larger disk may never sit on a smaller one. Each tower behaves like a stack.

How to use it

  • Choose 1–6 Disks and press New Puzzle.
  • To play by hand, click a source tower and then a target tower.
  • Next / Previous step through the optimal solution; Auto-Solve plays it back and Cancel stops it.

The recursive solution

hanoi(n, from, via, to): move n − 1 disks onto via, move the largest disk to to, then move the n − 1 disks on top of it.

The number of moves is T(n) = 2·T(n − 1) + 1 = 2n − 1, so the running time is O(2n) while the recursion depth is only n.