Home › Fun & Games › Tower of Hanoi

Tower of Hanoi

Move the whole tower from peg A to peg C, one disk at a time, never putting a bigger disk on a smaller one. Drag a disk or click two pegs, then see how close you got to the minimum number of moves.

Last updated:

Moves0
Minimum15
Time0:00
A 1
B 2
C 3 target

Drag a disk, or click a peg and then another peg.

Advertisement
Advertisement

How to use the Tower of Hanoi

  1. Pick the number of disks (3 is easy, 8 needs 255 moves) and press New game.
  2. Move a disk by dragging it to another peg, or click the peg you want to take from and then the peg you want to put it on. Keys 1, 2 and 3 work too.
  3. Follow the one rule: a disk can only go on an empty peg or on a larger disk.
  4. Rebuild the tower on peg C. Your move count is compared with the minimum possible, 2ⁿ − 1.
  5. Stuck? Press Hint for the best next move, Undo to take a move back, or Solve to watch the optimal solution from where you are.

Minimum moves for each number of disks

The fewest moves needed to move a tower of n disks is 2n − 1. It comes from the recurrence T(n) = 2·T(n − 1) + 1 with T(1) = 1: you move the smaller tower out of the way, move the biggest disk once, and move the smaller tower back on top.

DisksMinimum moves (2n − 1)Smallest disk movesLargest disk moves
3741
41581
531161
663321
7127641
82551281
101,0235121
6418,446,744,073,709,551,6152631

In an optimal solution disk k (counting from the smallest, k = 1) moves exactly 2n − k times, which is why the smallest disk moves on every second turn.

How to solve the Tower of Hanoi

The recursive method

  1. Move the top n − 1 disks from the start peg to the spare peg (using the target peg as the helper).
  2. Move the largest disk from the start peg to the target peg.
  3. Move the n − 1 disks from the spare peg onto the target peg (using the start peg as the helper).

Each of steps 1 and 3 is the same puzzle with one disk fewer, so you repeat the idea until only one disk is left. This is a classic first example of recursion in computer science.

The easy pattern to remember

  • Alternate moves. On odd-numbered moves (1st, 3rd, 5th…) move the smallest disk. On even-numbered moves make the only legal move that doesn't involve the smallest disk.
  • Keep the smallest disk going the same way. With an odd number of disks it cycles A → C → B → A…; with an even number it cycles A → B → C → A….
  • Disks on the same peg alternate parity. In an optimal solution an odd-numbered disk never lands directly on another odd-numbered disk, and even never on even.

Keyboard controls

KeyAction
1, 2, 3 (or A, B, C)Pick up from / drop onto that peg
EscPut the lifted disk back
Ctrl + ZUndo the last move
Tab, then EnterFocus a peg and select it

Frequently asked questions

What are the rules of the Tower of Hanoi?

All disks start stacked on one peg, largest at the bottom. You must move the whole stack to another peg, moving only one disk at a time (the top disk of a peg) and never placing a larger disk on top of a smaller one.

What is the minimum number of moves?

The minimum for n disks is 2^n − 1. That is 7 moves for 3 disks, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8. Every extra disk doubles the moves and adds one.

What is the trick to solving it?

Think recursively: to move n disks to a target peg, first move the top n − 1 disks to the spare peg, move the largest disk to the target, then move the n − 1 disks on top of it. A handy pattern is that the smallest disk moves on every other turn, always in the same direction around the pegs.

Which way should the smallest disk move first?

With an odd number of disks, move the smallest disk straight to the target peg first (A to C). With an even number, move it to the middle peg first (A to B). Following this makes the optimal solution easy to find.

Can the solver finish a game I already started?

Yes. Press Solve at any time and it computes the shortest sequence of moves from your current position to a complete tower on peg C, then plays it with animation. You can stop it whenever you like.

How long would 64 disks take?

In the legend of the temple puzzle, monks move 64 golden disks. That needs 2^64 − 1, or about 18.4 quintillion, moves. At one move per second it would take roughly 585 billion years.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

Sliding PuzzlePlay the 15 puzzle and 3×3 to 6×6 sliding tile puzzles with numbers or your own photo.Sudoku OnlinePlay free Sudoku with 4 difficulty levels, notes, hints and a timer.Mastermind GameCrack the secret 4-color code in 10 tries using black and white feedback pegs.2048 GamePlay 2048 online with swipe controls, undo and best score.SolitairePlay classic Klondike Solitaire with Draw 1 or Draw 3 and undo.Spider SolitairePlay Spider Solitaire online with 1, 2 or 4 suits, hints, unlimited undo and a timer.Play Chess vs ComputerPlay chess online against the computer at 8 difficulty levels.Connect FourPlay Connect 4 online against the computer or a friend.Snake GamePlay classic Snake online with speeds, wall modes and high scores.Tic Tac ToePlay tic tac toe against an unbeatable AI or a friend.Block Puzzle GameClassic falling blocks puzzle game with levels and high score.MinesweeperPlay classic Minesweeper: 3 difficulties, flags, timer and a safe first click.Word Guess GameGuess the hidden five-letter word in six tries. Unlimited games, stats and hard mode.HangmanClassic friendly hangman word game with categories, hints and difficulty levels.Memory Card GameFlip cards and match emoji pairs on 4×3 to 6×6 grids, with moves, timer and best scores.Flappy GameTap to flap a round chick through bamboo gaps and beat your high score.Dino Runner GameJump cacti and duck buzzing beetles in an original endless desert runner.Typing GameType the falling words before they land; level up and track your WPM and accuracy.Math Quiz GameTimed mental math rounds with four operations, five levels, streaks and high scores.Flag QuizGuess the flags of all 196 countries in four modes, from multiple choice to timed.Map QuizFind countries on a clickable world map or name the highlighted one, by continent.Periodic Table QuizLearn all 118 elements: quiz symbols, names and atomic numbers or click them on the table.Spelling Bee GameMake words from 7 honeycomb letters, always using the center letter. New daily puzzle.Jigsaw PuzzleTurn a sample picture or your own photo into a 12 to 150 piece jigsaw puzzle and solve it.Mahjong SolitaireMatch pairs of free tiles to clear the Turtle and two smaller layouts, with hints, shuffle and undo.Scrabble Word FinderFind the highest-scoring words from your rack, with blank tiles, patterns and bingo bonus.Wordle SolverEnter your guesses and colours to see every possible answer and the best next guess.Crossword SolverFind words that fit a crossword pattern like c?t from the length and the letters you know.Word Search GeneratorMake a word search puzzle from your words, play it online or print it with an answer key.Pong GamePlay classic Pong online against the computer on 3 levels or with a friend, first to 11.Breakout GameClassic brick breaker game with levels, power-ups and lives. Play with mouse, touch or keyboard.Whack-a-MoleWhack the moles before they hide in 30 or 60 second rounds. Speed increases, best score saved.Simon Says GameRepeat the growing color and sound sequence. Classic memory game with strict mode and high score.CheckersPlay checkers online against the computer or a friend, with kings, undo and hints.ReversiPlay Reversi (Othello rules) online vs a 3-level computer or a friend, with move hints.GomokuGet five in a row on a 15×15 board against a smart computer or a friend.Battleship GamePlace your fleet and sink the computer's ships in this classic naval guessing game.FreeCellPlay FreeCell solitaire online with the classic numbered deals, undo, hints and auto-move.Math Equation Guess GameNerdle-style math puzzle: find the hidden 8-character equation in six tries. Daily and unlimited.Maze Chase GameArcade maze game: eat every dot, grab power pellets and dodge four ghosts. Levels, lives and high score.Mini Golf GamePlay 18 holes of top-down mini golf: drag to putt past windmills, water and sand, with a scorecard.Connect the Dots Flow PuzzleDraw lines to join matching colored dots and fill the whole board without crossings. 5×5 to 12×12.Car Parking PuzzleSlide the blocking cars and trucks to free the red car. 120 graded levels with hints.Peg SolitaireJump pegs over each other until only one is left. English, European and triangle boards with hints.Pipes PuzzleRotate pipe tiles to connect every pipe to the source with no leaks. 5×5 to 15×15, wrap mode, timer.Pop It GameVirtual pop-it fidget toy with pop sounds, shapes and colors, plus a two-player last-pop game.Rebus PuzzlesPicture-word brain teasers like MIND over MATTER, with hints, answers and printable sets.Road Crossing GameFrogger-style arcade: hop across busy roads and a log-filled river, grab coins and fill all 5 homes.SokobanPush every box onto a goal in this warehouse puzzle. 60 levels, undo, swipe and custom levels.Spend a Billion Dollars GameSpend a billion (or any fortune) on houses, jets and good causes, then save your receipt.Spot the DifferenceFind 5 to 10 differences between two illustrated pictures, with new scenes every game.US States Visited MapClick US states to mark visited, lived or want to go, see your count out of 50 and save a PNG.Taboo Word Game CardsDescribe the word without saying the forbidden ones: 100+ cards, timer and team scores.Tangram PuzzleDrag, rotate and flip 7 tangram pieces to fill 58 silhouettes, with snapping, hints and kids mode.Ultimate Tic Tac ToePlay 9 nested tic-tac-toe boards vs a Monte Carlo AI or a friend, with a rules guide.US Map PuzzleDrag the 50 states onto the US map. Snapping, timer, labels toggle, regions and an easy kids mode.US Presidents QuizName all 47 US presidencies in order against the clock, match years and facts, or study them.War Card GamePlay the classic War card game vs the computer: flip cards, fight wars, auto-play at any speed.Word Connect GameSwipe letters on a wheel to make words and fill the mini crossword. Hundreds of levels, hints, coins.Word Scramble GameUnscramble jumbled words against the clock. Pick a theme and difficulty, build streaks, use hints.Word Tile Game vs ComputerPlay a crossword board game against the computer: premium squares, tile bag, scoring, dictionary.Chinese Chess (Xiangqi)Play Chinese chess vs the computer at 5 levels or with a friend, with legal move highlights.Maze GeneratorCreate random mazes in any size, solve them with the arrow keys, then print or save as PNG.Sudoku SolverType in any Sudoku and solve it instantly, get step-by-step hints and check it has one solution.Bingo Card GeneratorMake printable 75-ball or custom word bingo cards, from 1 to 200 unique cards, as PDF or print.Bingo Number GeneratorFree bingo caller for 75 or 90 balls with a live board, auto-call timer and spoken numbers.Online ScoreboardKeep score for any game: big team scores, game clock, periods, fullscreen and sport presets.Trivia Quiz300+ general knowledge trivia questions in 10 categories. 10-question rounds with a timer and score.
See all 141 Online Games →