Tower of Hanoi

Tower of Hanoi online

Move the whole wooden tower to peg C — one disk at a time, never a bigger disk on a smaller one. Choose 3 to 8 disks and try to match the minimum, 2ⁿ − 1 moves. Free, no sign-up, made for touch.

Tower of Hanoi · 3

Moves0Min7Time0:00Best—

Tap a peg to pick up its top disk, then tap another peg to drop it — or drag the disk across. Never put a bigger disk on a smaller one. Move the whole tower to peg C.

How to play Tower of Hanoi

  1. Pick the number of disks. 3 disks is a 7-move warm-up; every extra disk doubles the work, up to 255 moves for 8 disks. Each size keeps its own game and best score.
  2. Pick up the top disk. Tap a peg to lift its top disk (it rises above the peg), or press and drag that disk straight away.
  3. Drop it on another peg. Tap the peg you want, or let go of the dragged disk over it. A disk may only go on an empty peg or on a bigger disk.
  4. Move the whole tower to peg C. The puzzle is solved when every disk sits on the right-hand peg, biggest at the bottom. The counter shows your moves next to the minimum, 2ⁿ − 1.
  5. Use Undo, Hint or Solve when stuck. Undo takes back a move. Hint plays the best next move from wherever you are, and Solve plays the rest of the shortest solution so you can watch the pattern. Games with hints don’t count as a best.

Tower of Hanoi solution for 3 disks (7 moves)

Here is the shortest solution, move by move. Notice how the smallest disk moves on every other turn, and how the biggest disk moves exactly once, right in the middle.

ABC
Start
ABC
Move 1: disk 1 A→C
ABC
Move 2: disk 2 A→B
ABC
Move 3: disk 1 C→B
ABC
Move 4: disk 3 A→C
ABC
Move 5: disk 1 B→A
ABC
Move 6: disk 2 B→C
ABC
Move 7: disk 1 A→C

For 4 disks, first move the top three to the middle peg (7 moves), move disk 4 to C, then move the three back on top of it (7 more): 15 in total. Every size works the same way, which is why the count doubles and adds one each time.

Minimum moves for 3 to 8 disks

DisksMinimum moves (2ⁿ − 1)At 1 move per second
377 s
41515 s
53131 s
6631 min 3 s
71272 min 7 s
82554 min 15 s

The formula comes from the recursive idea: to move n disks you move n − 1 disks out of the way, move the biggest, and move the n − 1 back — so moves(n) = 2 × moves(n − 1) + 1, which works out to 2ⁿ − 1.

Tower of Hanoi strategy: two ways to think about it

  • Think big disk first (recursion). Ask “where must the biggest disk that isn’t home yet go?” Everything above it has to be stacked on the one remaining peg first. Repeat the same question for that smaller tower. This works from any position, which is exactly how our Hint button decides.
  • Follow the smallest disk (no thinking needed). Move the smallest disk every second turn, always round the pegs in the same direction; in between, make the only other legal move. With an odd number of disks the small one goes A→C→B→A…, with an even number A→B→C→A….
  • Never stack two disks of the same parity. In a perfect solution an odd-numbered disk is never placed straight onto another odd one (or even onto even). If you are about to, it is usually a wasted move.
  • Count to check yourself. Halfway through a perfect solve (after 2ⁿ⁻¹ − 1 moves) every disk except the biggest should be stacked on the middle peg.

Why teachers use the Tower of Hanoi

The puzzle was invented by the French mathematician Édouard Lucas and sold in 1883. It is a classic for teaching recursion in computer science, powers of two in maths, and planning ahead in psychology (the closely related Tower of London test). For children, 3 disks is a good start; once 7 moves feels easy, add a disk and talk about why the number roughly doubles.

Like it? Try more logic games that reward planning: peg solitaire (jump pegs until one is left), the 15 puzzle, the classic klotski sliding block, or our logic puzzles for kids. Chinese speakers can play the 汉诺塔 version of this page with the same saved games.

FAQ

What is the minimum number of moves for the Tower of Hanoi?

2ⁿ − 1 for n disks: 7 moves for 3 disks, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8. You can’t do better, because the biggest disk has to move at least once and, before it does, every smaller disk must be stacked on the third peg — which is the same puzzle with one disk fewer, done twice.

How do you solve the Tower of Hanoi with 3 disks?

Seven moves: small disk to C, middle to B, small to B, big to C, small to A, middle to C, small to C. In words: park the two small disks on the middle peg, move the big disk across, then rebuild the small pair on top of it.

Is there an easy rule that works for any number of disks?

Yes. Move the smallest disk on every odd move, always in the same direction round the pegs (A→C→B→A when the number of disks is odd, A→B→C→A when it is even). On every even move, make the only legal move that doesn’t touch the smallest disk. Our tests check that this rule gives exactly the 2ⁿ − 1 move solution for up to 10 disks.

What if I make a mistake — is the puzzle ruined?

No. Every arrangement of the disks can still be solved. Press Hint and the game plays the best move from your current position; the line under the board tells you how many moves are left at best. From any position you are never more than 2ⁿ − 1 moves away from the goal.

What is the legend of the 64 golden disks?

The puzzle was sold in 1883 with a story about priests moving 64 golden disks; when they finish, the world ends. At one move per second the minimum 18,446,744,073,709,551,615 moves would take about 585 billion years.

Does it work on a phone or iPad, and is my game saved?

Yes. The pegs are wide tap targets, dragging works with a finger, and your current game for each size, the timer and your best scores stay in this browser — no sign-up.