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
- The tower starts on the first peg, the largest disk at the bottom and the smallest at the top.
- Move one disk at a time: the top disk of one peg onto another peg.
- A disk can go on an empty peg or on a larger disk, never on a smaller one.
- 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 movesEach 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.
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, a new puzzle at every press, with notes and hints.
Killer Sudoku
Killer sudoku online, easy to expert, cages and their sums, with hints.
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
The picture sudoku for kids: a cat, a frog, an elephant and 21 more animals where the numbers would be.
Dino Sudoku
A T-Rex, a triceratops, a volcano and a dino egg: a dinosaur puzzle that grows from 4×4 to 9×9.
Fruit Sudoku
Apples, bananas and dragon fruit fill the rows: a fruit puzzle game, from a quick 4×4 up.
Flower Sudoku
Tulips, sunflowers and irises, no two alike in a row: a flower puzzle in full bloom.
Ocean Sudoku
A whale, a crab, a seahorse and a starfish: the sea in a sudoku, from 4×4 to 9×9.
Vehicle Sudoku
A bus, a plane, a sailboat and a fire engine on the grid: the car puzzle game for road-trip fans.
Vegetable Sudoku
Carrot, broccoli, corn and tomato in every row: a vegetable puzzle from the garden patch.
Mushroom Sudoku
A morel and 23 more from the forest floor, told apart by shape: a mushroom puzzle for walkers.
Bird Sudoku
A flamingo, a toucan, an owl and a parrot, each in its own place: a bird puzzle to fly through.
Bug Sudoku
A ladybug, a bee, a butterfly and a snail crawl through the rows: the bug sudoku.
Music Sudoku
Piano, guitar, drum and trumpet: a band of 24 instruments, each playing once per row.
Bakery Sudoku
Croissants, cupcakes, donuts and pretzels: a food puzzle fresh from the oven.
Robot Sudoku
Twenty-four friendly robots, each one built differently: a robot puzzle from 4×4 to 9×9.
Zodiac Sudoku
All twelve signs, from Aries to Pisces: your star sign in a sudoku.
Christmas Sudoku
A tree, a snowman, a reindeer and the presents: the Christmas puzzle for December evenings.
Thanksgiving Sudoku
A turkey, a pumpkin, pecan pie and a squirrel: the Thanksgiving puzzle for the whole table.
Valentine Sudoku
Roses, a love letter, a teddy bear and a ring: a Valentine's Day puzzle for two.
Sports Sudoku
Football, basketball, tennis and a trophy: 24 balls and pieces of gear, one per row.
Gym Sudoku
Dumbbells, kettlebells and a punching bag: a workout for the brain, between sets.
Tools Sudoku
A hammer, a drill, pliers and a toolbox: a DIY puzzle for the workshop.
Letter Sudoku
Letter sudoku online: an alphabet sudoku with letters in place of numbers, and one row spells a hidden word.