Skip to the content

Puzzle Games

Tower of Hanoi Game: Play the Puzzle Online, Solution and Formula

Play the Tower of Hanoi online, free: move the whole tower from the first peg to the last, one disk at a time, never a larger disk on a smaller one. With n disks the puzzle takes at least 2n − 1 moves, 15 for the 4 you start with.

Disks / Difficulty
Moves
0
Minimum
15
Best
–

Move the whole tower from peg A to peg C. Drag the top disk to another peg, or tap a peg, then the peg it goes to.

How to play the Tower of Hanoi: the rules

  1. The tower starts on the first peg, the largest disk at the bottom and the smallest at the top.
  2. Move one disk at a time: the top disk of one peg onto another peg.
  3. A disk can go on an empty peg or on a larger disk, never on a smaller one.
  4. The puzzle is solved when the whole tower stands on the last peg, in the same order.

The Tower of Hanoi formula: the minimum number of moves

A tower of n disks needs at least 2n − 1 moves. Each disk more doubles the moves, and one more.

  • 73 disks
  • 154 disks
  • 315 disks
  • 636 disks
  • 1277 disks
  • 2558 disks

The Tower of Hanoi solution, step by step

Think of the largest disk

It can only move when every other disk is out of its way, stacked on the middle peg. So to move a tower of 4 disks: move the 3 above it to the middle peg, move the largest to the last peg, then move the 3 back on top of it. Moving the 3 is the same puzzle, one disk smaller, and so on down to a single disk.

Or follow the smallest disk

Move the smallest disk on every other move, always round the pegs in the same direction (with an odd number of disks, first to the last peg; with an even number, first to the middle one). On the moves in between, make the one move that does not touch the smallest disk. It gives the same shortest solution.

Stuck? Press Hint above: it shows the next move of the shortest solution from where your tower stands.

The Tower of Hanoi algorithm

The classic example of recursion: to move n disks, move n − 1 disks to the spare peg, move the largest disk, then move the n − 1 disks onto it. A tower of 0 disks needs no move, which ends it. In Python:

def hanoi(n, source="A", target="C", spare="B"):
    if n == 0:
        return
    hanoi(n - 1, source, spare, target)
    print(f"Move disk {n} from {source} to {target}")
    hanoi(n - 1, spare, target, source)

hanoi(4)  # 15 moves

Each call makes two calls on one disk fewer, so the moves follow M(n) = 2 M(n − 1) + 1, with M(1) = 1: the formula 2n − 1.

The history and the legend

The French mathematician Édouard Lucas published the puzzle in 1883 as a toy, under the name N. Claus de Siam, an anagram of Lucas d'Amiens. It came with a legend: in a temple, priests move a tower of 64 golden disks by the same rules, and when they finish, the world will end.

18,446,744,073,709,551,615moves for 64 disks: about 585 billion years at one move a second

Questions

What is the minimum number of moves for 3 disks?

Seven. In general a tower of n disks needs 2 to the power n, minus 1 moves: 3 for two disks, 7 for three, 15 for four, 31 for five, 63 for six.

How many moves for 4 disks?

Fifteen: move the top three disks to the middle peg (7 moves), the largest disk to the last peg (1 move), and the three back on top of it (7 moves).

Who invented the Tower of Hanoi?

The French mathematician Édouard Lucas, who sold it as a toy in 1883 under the name N. Claus de Siam, an anagram of Lucas d'Amiens.

What is the trick to solve it?

Move the smallest disk on every other move, always in the same direction round the pegs. In between, make the only other move the rules allow. That is the shortest solution, for any number of disks.

How long would 64 disks take?

It would take 18,446,744,073,709,551,615 moves. At one move a second, without a mistake, that is about 585 billion years.

Can every Tower of Hanoi be solved?

Yes. Every tower can be moved, whatever the number of disks, and there is exactly one shortest way to do it.

More puzzle games

  • Sliding Puzzle

    The 15 puzzle in 3×3, 4×4 and 5×5, and a solver for any sliding puzzle you set up.

  • Sudoku

    Easy to expert sudoku with notes and hints, and a solver that fills any sudoku cell by cell.

  • Killer Sudoku

    Sudoku with cages and their sums, easy to expert, and a solver for any killer sudoku you draw.

  • Nonogram

    Nonograms from 5×5 to 20×20, a picture to fill from its clues, and a solver for any nonogram's clues.

  • Minesweeper

    Easy to hard minesweeper you never have to guess on, the patterns, and a solver for any field you copy.