Moves: 0
Optimal: 7
Time: 0:00

Recent Moves

No moves yet

Game Setup

Auto Solve

Recursive Solution

To move n disks from source to destination:

  1. Move n-1 disks from source to auxiliary tower
  2. Move the largest disk from source to destination
  3. Move n-1 disks from auxiliary to destination
function hanoi(n, from, to, aux) {
  if (n == 1) move(from, to);
  else {
    hanoi(n-1, from, aux, to);
    move(from, to);
    hanoi(n-1, aux, to, from);
  }
}

Mathematical Analysis

Recurrence Relation:

T(n) = 2T(n-1) + 1, T(1) = 1

Solution:

T(n) = 2ⁿ - 1

Examples:

  • 3 disks: 2³ - 1 = 7 moves
  • 4 disks: 2⁴ - 1 = 15 moves
  • 5 disks: 2⁵ - 1 = 31 moves
  • 8 disks: 2⁸ - 1 = 255 moves

Instructions

How to Play

• Move all disks from Tower 1 to Tower 3

• Only one disk can be moved at a time

• A larger disk cannot be placed on a smaller disk

• Click and drag disks or use manual controls

Controls

• Mouse: Click and drag disks

• Keyboard: 1,2,3 to select/move towers

• Space: Toggle disk selection

• R: Reset game

Auto Solve

• Step through the recursive algorithm

• Watch how subproblems are solved

• Visualize recursion in action