How to play the Tower of Hanoi
- Choose how many discs you want (3 to 8). They all start stacked on peg 1, the largest at the bottom and the smallest on top.
- Tap a peg to pick its top disc, then tap the peg you want to move it to. You can also drag the disc to the other peg.
- Move one disc at a time and never put a disc on top of a smaller one.
- You win when the whole tower ends up on another peg. The counter shows your moves and the minimum possible; if you get stuck, the Solve button plays back the shortest solution.
A classic of logic and recursion
The Tower of Hanoi is a mathematical puzzle invented by the Frenchman Édouard Lucas in 1883. With n discs, the minimum number of moves is 2 to the power of n minus 1: 7 with three discs, 15 with four, 31 with five and 255 with eight. Every disc you add doubles the work and adds one.
There is a systematic way to solve it. To move a tower of n discs, first move the top n−1 discs to the spare peg, then move the largest disc to the destination and finally move the n−1 discs on top of it. It is a typical example of recursion. Another rule of thumb: the smallest disc moves on every odd move, always in the same direction (with an odd number of discs it goes from peg 1 to 3, from 3 to 2 and from 2 to 1; with an even number, from 1 to 2, from 2 to 3 and from 3 to 1); on the even moves there is only one possible move between the other two pegs.
Legend has it that monks are moving a tower of 64 golden discs; it would take more than 18 quintillion moves, hundreds of billions of years at one per second. Everything is played in your browser: nothing is sent to any server and only your best result is saved on your device.
Things to keep in mind
- The record (fewest moves) is saved per number of discs only in this browser: if you clear the site data, it is lost.
- If you use Solve, the game does not count towards the record. The button calculates the best solution from the position you are in, not from the beginning.
- With 8 discs at least 255 moves are needed; the automatic solution plays them back at a good pace, and you can stop it at any moment.
Frequently asked questions
What is the goal of the Tower of Hanoi?
To move all the discs from peg 1 to another peg, one at a time, never putting a large disc on a smaller one.
How many moves are needed at minimum?
2 to the power of the number of discs, minus 1: 7 with 3 discs, 15 with 4, 31 with 5, 63 with 6, 127 with 7 and 255 with 8. The counter at the top shows the minimum for your game.
How do I move the discs on a phone?
Tap the source peg and then the destination peg, or drag the top disc with your finger to the other peg. On a computer you can also use keys 1, 2 and 3.
What is the Solve button for?
It plays back the shortest solution step by step from the current position. It is useful for learning the pattern; then you can start a new game and try it yourself.
Is there a trick to avoid getting lost?
Yes: the smallest disc moves on the odd moves and always in the same direction; on the even moves there is only one legal move between the other two pegs.
Is it free? Do I need to install anything?
It is free, needs no sign-up or installation and works on phones and computers. No data is uploaded to any server.