IQ Metrics
IIF Certified Assessment Start IQ Test
IIF Certified Assessment
Tower of Hanoi game icon

Brain Games · Planning

Tower of Hanoi

Move the tower from peg A to peg C, one disk at a time, never a larger disk on a smaller one. 7 moves is the minimum.

No signup · 2 to 5 minutes · Keys 1, 2, 3 and Ctrl+Z work too

In short

How does the Tower of Hanoi puzzle work?

The Tower of Hanoi is a puzzle with three pegs and a stack of disks, largest at the bottom. You move the whole tower to another peg, one disk at a time, and never put a bigger disk on a smaller one. The fewest moves for n disks is 2ⁿ − 1: 7 for 3 disks, 15 for 4.

Below: three ways to play the Tower of Hanoi game online, move-by-move solutions, the algorithm in code, and what the puzzle says about planning.

2ⁿ − 1The fewest moves for n disks: 7 for 3 disks, 255 for 8
3 rulesOne disk at a time, only a top disk, never a larger disk on a smaller one
1883The year Édouard Lucas published the puzzleas "N. Claus (de Siam)"
585 billionYears that 64 disks would take, at one move a second

How to play

Three ways to play the Tower of Hanoi

Pick a mode at the top and press Start. Tap a peg, then the peg to move to, or drag a disk. The game counts every move and shows how far you are from the minimum.

Classic

Planning several moves ahead.

  1. Move the whole tower from peg A to peg C.
  2. Move one disk at a time, and never put a larger disk on a smaller one.
  3. Tap a peg, then the peg to move to, or drag the disk. Hint and Show me work from any position.
Play Classic

Perfect run

Planning without a safety net.

  1. The same tower, with no Undo, Hint or Show me.
  2. Every move must be on the shortest path: 7 moves for 3 disks.
  3. One move off the path and the run is no longer perfect, but you can still finish.
Play Perfect run

Scramble

Planning from a position you have never seen.

  1. The disks start on random pegs, always stacked by size.
  2. Gather them all on the outlined peg.
  3. The game works out the minimum for that start. The daily puzzle is the same for everyone.
Play Scramble

The numbers

Minimum moves for 3 to 10 disks

Each extra disk doubles the moves and adds one: the minimum for n disks is 2ⁿ − 1.

DisksFewest moves
3 disks7
4 disks15
5 disks31
6 disks63
7 disks127
8 disks255
9 disks511
10 disks1,023

The bars grow one step per disk because the moves double. 64 disks need 18,446,744,073,709,551,615 moves.

The trick

How to solve it, for any number of disks

Every solution is the same three steps. To move n disks from A to C:

  1. Move the top n − 1 disks from A to B, using C as the spare.
  2. Move the largest disk from A to C.
  3. Move the n − 1 disks from B to C, using A as the spare.
  4. The shortcut: move the smallest disk every other move, always the same way round: A → C → B → A for an odd number of disks, A → B → C → A for an even number. In between, make the only other legal move.

Solutions

The solutions, move by move

Peg A is the start, B the spare and C the goal.

3 disks · 7 moves

  1. A → C
  2. A → B
  3. C → B
  4. A → C
  5. B → A
  6. B → C
  7. A → C

4 disks · 15 moves

  1. A → B
  2. A → C
  3. B → C
  4. A → B
  5. C → A
  6. C → B
  7. A → B
  8. A → C
  9. B → C
  10. B → A
  11. C → A
  12. B → C
  13. A → B
  14. A → C
  15. B → C

5 disks · 31 moves

  1. A → C
  2. A → B
  3. C → B
  4. A → C
  5. B → A
  6. B → C
  7. A → C
  8. A → B
  9. C → B
  10. C → A
  11. B → A
  12. C → B
  13. A → C
  14. A → B
  15. C → B
  16. A → C
  17. B → A
  18. B → C
  19. A → C
  20. B → A
  21. C → B
  22. C → A
  23. B → A
  24. B → C
  25. A → C
  26. A → B
  27. C → B
  28. A → C
  29. B → A
  30. B → C
  31. A → C

For programmers

The Tower of Hanoi in code

The three steps are a recursion: the solution for n disks uses the one for n − 1 disks twice. So T(n) = 2T(n − 1) + 1, which gives T(n) = 2ⁿ − 1.

JavaScript
function hanoi(n, from, to, spare) {
  if (n === 0) return;
  hanoi(n - 1, from, spare, to);
  console.log('Move disk ' + n + ' from ' + from + ' to ' + to);
  hanoi(n - 1, spare, to, from);
}
hanoi(3, 'A', 'C', 'B'); // 7 moves
Python
def hanoi(n, source, target, spare):
    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(3, "A", "C", "B")  # 7 moves
Java
public class Hanoi {
    static void hanoi(int n, char from, char to, char spare) {
        if (n == 0) return;
        hanoi(n - 1, from, spare, to);
        System.out.println("Move disk " + n + " from " + from + " to " + to);
        hanoi(n - 1, spare, to, from);
    }

    public static void main(String[] args) {
        hanoi(3, 'A', 'C', 'B'); // 7 moves
    }
}
C
#include <stdio.h>

void hanoi(int n, char from, char to, char spare) {
    if (n == 0) return;
    hanoi(n - 1, from, spare, to);
    printf("Move disk %d from %c to %c\n", n, from, to);
    hanoi(n - 1, spare, to, from);
}

int main(void) {
    hanoi(3, 'A', 'C', 'B'); /* 7 moves */
    return 0;
}

Rules and formula

What are the rules and the formula?

There are three rules and one formula: the fewest moves for n disks is 2ⁿ − 1. Each extra disk doubles the moves and adds one.

The three rules

Move one disk at a time. Take only the top disk of a peg. Never place a disk on a smaller one. That is the whole puzzle.

Why the formula is 2ⁿ − 1

To move n disks, move the n − 1 disks above the largest out of the way, move the largest once, then put the n − 1 disks back on top. So the count doubles and adds one: 1, 3, 7, 15, 31.

The equation at work

For 5 disks, 2⁵ − 1 = 31 moves. For 8 disks, the most in our game, it is 255. The legend’s 64 disks need about 18.4 quintillion moves.

Algorithm

How does the Tower of Hanoi algorithm work?

The classic Tower of Hanoi algorithm is recursive: to move a tower, move the smaller tower above its bottom disk, move that disk, then move the smaller tower back on top. It always finds the shortest solution.

Without recursion

A simple loop works too. On every odd move, shift the smallest disk one peg along, always the same way round. On every even move, make the only legal move that does not touch it.

Which way round?

With an odd number of disks, the smallest disk heads straight for the target peg first. With an even number, it goes to the spare peg first. Get this wrong and you finish on the wrong peg.

In JavaScript, Python and Java

The code section above has the recursive version in all three languages, including a short Tower of Hanoi in Java. Each one prints the same 7 moves for 3 disks.

The recursive solution: n disks from peg A to peg C

  1. If n is 0, stop: there is nothing to move.
  2. Move the top n − 1 disks from A to B, using C as the spare.
  3. Move the largest disk from A to C.
  4. Move the n − 1 disks from B to C, using A as the spare.

Complexity

What is the time complexity of the Tower of Hanoi?

The time complexity of the recursive solution is O(2ⁿ), because it makes 2ⁿ − 1 moves. It needs O(n) extra memory: one level of recursion for each disk.

Why it doubles

Moving n disks means moving n − 1 disks twice, plus one move. So T(n) = 2T(n − 1) + 1, and every extra disk doubles the work.

Can it be faster?

No. With three pegs, 2ⁿ − 1 moves is the proven minimum, so no algorithm can solve it in fewer moves. Only more pegs change that.

Pseudocode

hanoi(n, from, to, spare): if n is 0, stop; hanoi(n − 1, from, spare, to); move disk n from “from” to “to”; hanoi(n − 1, spare, to, from).

History

Where does the Tower of Hanoi come from?

The French mathematician Édouard Lucas published it in 1883 under the name N. Claus (de Siam), an anagram of Lucas d’Amiens. Its booklet told a legend of priests moving 64 golden disks in a temple at Benares.

The legend

The legend was part of the toy’s story, not ancient history. At one move a second, 64 disks would take about 585 billion years.

The Tower of Hanoi test in psychology

Psychologists use tower puzzles to study planning. The Tower of Hanoi-Revised and the Tower of London are research versions (Welsh and Huizinga, 2001; Shallice, 1982).

Tower of Hanoi with 5 disks

Five disks need 31 moves. The full move-by-move list is in the solutions above, after 3 and 4 disks.

Skills

What the puzzle uses

Planning

Seeing several moves ahead: each disk waits until the smaller ones are out of the way. Psychologists use the Tower of London, a close cousin, to study planning.

Working memory

Keeping track of where the smaller disks have to go while you move the larger ones.

Pattern spotting

Noticing the rhythm: the smallest disk moves every other move. Once you see it, any tower falls.

And IQ

Is the Tower of Hanoi an IQ test?

No, but it is a classic test of planning. Psychologists use tower puzzles to study how people think ahead, and a close cousin, the Tower of London, was built for exactly that (Shallice, 1982).

Planning is an executive function, and executive function and IQ overlap without being the same. The closest IQ area is fluid reasoning: solving new problems. To see your reasoning, memory and speed in one score, take the IQ test.

The five areas of a modern IQ test

  1. Verbal comprehensionWord knowledge and reasoning in words
  2. Visual spatialSeeing and turning shapes in your mind
  3. Fluid reasoningFinding the rule in a new problemClosest to this game
  4. Working memoryHolding information and using itAlso used
  5. Processing speedQuick, accurate simple work

The five primary index areas of the WISC-V (children, 2014) and the WAIS-5 (adults, 2024). See WAIS-5 index scores explained.

FAQ

Tower of Hanoi: questions people ask

What is the Tower of Hanoi?

A puzzle with three pegs and a stack of disks in size order. Move the whole stack to another peg, one disk at a time, never putting a larger disk on a smaller one.

What is the minimum number of moves?

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.

Why is the minimum 2ⁿ − 1?

To move the largest disk, the n − 1 disks above it must first go to the spare peg, and afterwards they move back on top of it. So T(n) = 2T(n − 1) + 1 with T(1) = 1, which gives T(n) = 2ⁿ − 1.

Is there a trick to solving it?

Yes. Move the smallest disk every other move, always the same way round: A → C → B → A for an odd number of disks, A → B → C → A for an even number. On the moves in between, only one legal move does not use the smallest disk.

How long would 64 disks take?

2⁶⁴ − 1 moves, about 18.4 quintillion. At one move a second that is about 585 billion years, roughly 42 times the age of the universe.

Why is it used to teach recursion?

The solution for n disks is built from the solution for n − 1 disks, used twice. That is recursion, and the program is only five lines long.

Who invented the Tower of Hanoi?

The French mathematician Édouard Lucas, in 1883. He published it under the name N. Claus (de Siam), an anagram of Lucas d’Amiens, with a legend about priests moving 64 golden disks.

Does solving it mean a high IQ?

No. It is a planning puzzle, and practice and the smallest-disk trick make anyone faster. A full IQ test measures reasoning across many kinds of questions.

What is Scramble mode?

The disks start on random pegs, always stacked by size, and you gather them on the outlined peg. The game works out the minimum for that start, so a perfect run is still possible. The daily puzzle is the same for everyone.

Do undos count as moves?

Every move counts, and Undo never removes one. A run is Perfect when it uses the minimum number of moves with no undo and no hint.

What about four pegs?

With four pegs the puzzle needs far fewer moves. The Frame–Stewart method is the best known, and it was proved optimal for four pegs in 2014.

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

Move the smallest disk to the spare peg first, not the target: with an even number of disks, that is the right start. Then follow the 15-move solution listed above.

Can I challenge a friend?

Yes. After a run, send the link: your friend plays the same tower, or the same Scramble puzzle, and sees both scores.

Is it free?

Yes. It is free to play, with no signup, and your scores stay in your browser.

Sources

The research behind this page

  1. Shallice, T. (1982). Specific impairments of planning. Philosophical Transactions of the Royal Society of London B, 298, 199–209. doi:10.1098/rstb.1982.0082
  2. Simon, H. A. (1975). The functional equivalence of problem solving skills. Cognitive Psychology, 7(2), 268–288. doi:10.1016/0010-0285(75)90012-2
  3. Welsh, M. C., & Huizinga, M. (2001). The development and preliminary validation of the Tower of Hanoi-Revised. Assessment, 8(2), 167–176. doi:10.1177/107319110100800205

The full picture

Your planning is one piece. Now see the whole picture.

Each game looks at one skill. The IQ test brings speed, reasoning and memory together in one score, with your percentile and a certificate.

Find Your IQ Score

Instant results · 10–20 minutes