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.

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

    A sliding tile puzzle online: the 8 puzzle, 15 puzzle and 24 puzzle, and a sliding puzzle solver.

  • Sudoku

    Free sudoku online, easy to expert, with notes and hints, and a sudoku solver for any grid.

  • Killer Sudoku

    Killer sudoku online, easy to expert, cages and their sums, and a killer sudoku solver.

  • Nonogram

    Nonograms online from 5×5 to 20×20, a picture to fill from its clues, and a nonogram solver.

  • Minesweeper

    Minesweeper online, easy to hard, never a guess, the patterns, and a minesweeper solver.

  • Animal Sudoku

    Animal sudoku online: a picture sudoku with 24 animals, 4x4 easy, 9x9 medium and hard, with hints.

  • Dino Sudoku

    Dino sudoku online: a dinosaur puzzle game, a picture sudoku with 24 dinosaurs, 4x4 easy, 9x9 medium and hard.

  • Fruit Sudoku

    Fruit sudoku online: a fruit puzzle game, a picture sudoku with 24 fruits, 4x4 easy, 9x9 medium and hard.

  • Flower Sudoku

    Flower sudoku online: a flower puzzle game, a picture sudoku with 24 flowers, 4x4 easy, 9x9 medium and hard.

  • Ocean Sudoku

    Ocean sudoku online: an ocean puzzle game, a picture sudoku with 24 sea creatures, 4x4 easy, 9x9 medium and hard.

  • Vehicle Sudoku

    Vehicle sudoku online: a car puzzle game, a picture sudoku with 24 vehicles, 4x4 easy, 9x9 medium and hard.

  • Vegetable Sudoku

    Vegetable sudoku online: a vegetable puzzle game, a picture sudoku with 24 vegetables, 4x4 easy, 9x9 medium and hard.

  • Mushroom Sudoku

    Mushroom sudoku online: a mushroom puzzle game, a picture sudoku with 24 mushrooms, 4x4 easy, 9x9 medium and hard.

  • Bird Sudoku

    Bird sudoku online: a bird puzzle game, a picture sudoku with 24 birds, 4x4 easy, 9x9 medium and hard.

  • Bug Sudoku

    Bug sudoku online: a bug puzzle game, a picture sudoku with 24 bugs, 4x4 easy, 9x9 medium and hard.

  • Music Sudoku

    Music sudoku online: a musical instrument puzzle game, a picture sudoku with 24 instruments, 4x4 easy, 9x9 medium and hard.

  • Bakery Sudoku

    Bakery sudoku online: a food puzzle game, a picture sudoku with 24 breads, cakes and pastries, 4x4 easy, 9x9 medium and hard.

  • Robot Sudoku

    Robot sudoku online: a robot puzzle game, a picture sudoku with 24 robots, 4x4 easy, 9x9 medium and hard.

  • Zodiac Sudoku

    Zodiac sudoku online: a zodiac signs puzzle game, a picture sudoku with the twelve signs, 4x4 easy, 9x9 medium and hard.

  • Christmas Sudoku

    Christmas sudoku online: a Christmas puzzle game, a picture sudoku with 24 Christmas pictures, 4x4 easy, 9x9 medium and hard.

  • Valentine Sudoku

    Valentine sudoku online: a Valentine's Day puzzle game, a picture sudoku with 12 pictures, 4x4 easy, 9x9 medium and hard.

  • Sports Sudoku

    Sports sudoku online: a sports puzzle game, a picture sudoku with 24 balls and pieces of gear, 4x4 easy, 9x9 medium and hard.

  • Gym Sudoku

    Gym sudoku online: a gym puzzle game, a picture sudoku with 12 weights and machines, 4x4 easy, 9x9 medium and hard.

  • Tools Sudoku

    Tools sudoku online: a tools puzzle game, a picture sudoku with 24 tools, 4x4 easy, 9x9 medium and hard.

  • Letter Sudoku

    Letter sudoku online: an alphabet sudoku with letters in place of numbers, and one row spells a hidden word.