Tower of Hanoi Solver for Google Sheetsβ„’

Tower of Hanoi Solver will soon be available as a
Google Workspace Marketplace Add-on.

For transparency and compliance:

 

 

 

 

 

 

 

When I first encountered recursion in my first-year Computer Science course at UC Berkeley, I remember feeling completely lost. The Tower of Hanoi felt like a trick: how could a solution even begin if every step depended on solving a smaller one first?

I took CS 3: Introduction to Symbolic Programming with Oliver Grillmeyer, who patiently helped us trace how pegs dynamically switch roles β€” source becomes helper, helper becomes target β€” and showed us that recursion isn’t magic. It’s structure. Still, it took me years to feel that structure rather than follow it blindly.

Understanding recursion, for me, was itself a recursive process: breaking the confusion into smaller, solvable questions. One insight led to another, and eventually something resembling clarity appeared.

🧱 From Idea to Interactive Simulation

Many years later β€” as an MYP Design teacher β€” I decided to revisit Tower of Hanoi, but this time through the lens of pedagogy. Could I help students not only solve the puzzle but see recursion as it unfolds?

Together with ChatGPT, I built a full Tower of Hanoi simulator inside Google Sheets, powered entirely by Apps Script. What made the project possible was not raw code, but a design-first conversation with AI:

What should students see? How should each move feel? How can the UI help build computational intuition? Once the experience was clearly mapped out, scripting followed naturally.Β 

πŸ› οΈ From Merged Disks to Painted Pegs

At first, I used merged cells to represent disks: larger disks spanned more columns, smaller disks fewer, all centered on pegs. It looked great β€” until I tried to move them.

Each step required breaking and re-merging cells dynamically. It was slow, fragile, and unsustainable.

So I pivoted: instead of merged cells, I painted rectangular blocks of adjacent cells, styled with consistent spacing and color. This approach:

  • Simplified disk creation and movement
  • Improved performance
  • Made animation smooth and glitch-free

βœ… Core Features

Here’s what the final simulator includes:

  • πŸ“ Tower rendered in a dynamic grid, adjustable from 1 to 10 disks
  • 🎨 Branded disk colors (hex-coded palette)
  • 🧱 β€œCreate Tower” button builds the stack interactively
  • πŸ” β€œSolve Tower” animates the recursive steps live
  • πŸ›‘ Kill Switch (Pause) checkbox to stop long simulations
  • 🧭 Sidebar UI with dropdown, buttons, and color-coded disk count
  • πŸ“– Instruction dialog, with zoom-out tips and clear guidance
  • πŸ”§ Gridline toggle, to switch between aesthetic and structural views
  • πŸ“Š Logging, toast messages, and pegs that visually rebuild in real time

Watch recursion come to life, one move at a time.

πŸ” Recursion, Visualized Step-by-Step


(Quick terminology: the source peg is the starting peg where all disks begin, the auxiliary peg is a temporary helper peg, and the target peg is where all disks should end up.)

Once the interface worked, I implemented recursion faithfully and visibly.

For those new to Tower of Hanoi: solving it recursively always follows the same structure:

  1. Move nβˆ’1 disks from source to auxiliary
  2. Move the nth (largest) disk to target
  3. Move nβˆ’1 disks from auxiliary to target

With every recursive call, the roles of the pegs rotate. This is the key to recursive thinking β€” the problem doesn’t just repeat, it reorganizes. In our code, I didn’t pre-generate moves. Each move happened only when its subproblem had been resolved. The simulation feels like how humans approach the problem β€” slowly, logically, recursively.

🧠 Recursive Move Breakdown for 1–11 Disks:

For 1 disk:

– Only 1 move: Source β†’ Target

– T(1) = 1

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

For 2 disks:

– To move 2 disks:

  1. Move 1 disks from Source to Helper β†’ T(1) moves
  2. Move disk 2 to Target β†’ 1 move
  3. Move 1 disks from Helper to Target β†’ T(1) moves

T(2) = T(1) + 1 + T(1) = 2Γ—T(1) + 1

Β Β Β Β Β Β Β = 2Γ—(2 – 1) + 1

Β Β Β Β Β Β Β = 2 + 1 = 3

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

For 3 disks:

– To move 3 disks:

  1. Move 2 disks from Source to Helper β†’ T(2) moves
  2. Move disk 3 to Target β†’ 1 move
  3. Move 2 disks from Helper to Target β†’ T(2) moves

T(3) = T(2) + 1 + T(2) = 2Γ—T(2) + 1

Β Β Β Β Β Β Β = 2Γ—(4 – 1) + 1

Β Β Β Β Β Β Β = 6 + 1 = 7

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

For 4 disks:

– To move 4 disks:

  1. Move 3 disks from Source to Helper β†’ T(3) moves
  2. Move disk 4 to Target β†’ 1 move
  3. Move 3 disks from Helper to Target β†’ T(3) moves

T(4) = T(3) + 1 + T(3) = 2Γ—T(3) + 1

Β Β Β Β Β Β Β = 2Γ—(8 – 1) + 1

Β Β Β Β Β Β Β = 14 + 1 = 15

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━For 5 disks:

– To move 5 disks:

  1. Move 4 disks from Source to Helper β†’ T(4) moves
  2. Move disk 5 to Target β†’ 1 move
  3. Move 4 disks from Helper to Target β†’ T(4) moves

T(5) = T(4) + 1 + T(4) = 2Γ—T(4) + 1

Β Β Β Β Β Β Β = 2Γ—(16 – 1) + 1

Β Β Β Β Β Β Β = 30 + 1 = 31

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━For 6 disks:

– To move 6 disks:

  1. Move 5 disks from Source to Helper β†’ T(5) moves
  2. Move disk 6 to Target β†’ 1 move
  3. Move 5 disks from Helper to Target β†’ T(5) moves

T(6) = T(5) + 1 + T(5) = 2Γ—T(5) + 1

Β Β Β Β Β Β Β = 2Γ—(32 – 1) + 1

Β Β Β Β Β Β Β = 62 + 1 = 63

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━For 7 disks:

– To move 7 disks:

  1. Move 6 disks from Source to Helper β†’ T(6) moves
  2. Move disk 7 to Target β†’ 1 move
  3. Move 6 disks from Helper to Target β†’ T(6) moves

T(7) = T(6) + 1 + T(6) = 2Γ—T(6) + 1

Β Β Β Β Β Β Β = 2Γ—(64 – 1) + 1

Β Β Β Β Β Β Β = 126 + 1 = 127

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━For 8 disks:

– To move 8 disks:

  1. Move 7 disks from Source to Helper β†’ T(7) moves
  2. Move disk 8 to Target β†’ 1 move
  3. Move 7 disks from Helper to Target β†’ T(7) moves

T(8) = T(7) + 1 + T(7) = 2Γ—T(7) + 1

Β Β Β Β Β Β Β = 2Γ—(128 – 1) + 1

Β Β Β Β Β Β Β = 254 + 1 = 255

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

For 9 disks:

– To move 9 disks:

  1. Move 8 disks from Source to Helper β†’ T(8) moves
  2. Move disk 9 to Target β†’ 1 move
  3. Move 8 disks from Helper to Target β†’ T(8) moves

T(9) = T(8) + 1 + T(8) = 2Γ—T(8) + 1

Β Β Β Β Β Β Β = 2Γ—(256 – 1) + 1

Β Β Β Β Β Β Β = 510 + 1 = 511

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━For 10 disks:

– To move 10 disks:

  1. Move 9 disks from Source to Helper β†’ T(9) moves
  2. Move disk 10 to Target β†’ 1 move
  3. Move 9 disks from Helper to Target β†’ T(9) moves

T(10) = T(9) + 1 + T(9) = 2Γ—T(9) + 1

Β Β Β Β Β Β Β = 2Γ—(512 – 1) + 1

Β Β Β Β Β Β Β = 1022 + 1 = 1023

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━For 11 disks:

– To move 11 disks:

  1. Move 10 disks from Source to Helper β†’ T(10) moves
  2. Move disk 11 to Target β†’ 1 move
  3. Move 10 disks from Helper to Target β†’ T(10) moves

T(11) = T(10) + 1 + T(10) = 2Γ—T(10) + 1

Β Β Β Β Β Β Β = 2Γ—(1024 – 1) + 1

Β Β Β Β Β Β Β = 2046 + 1 = 2047

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

πŸ§ͺ Proof by Induction: Why T(nβˆ’1) = 2ⁿ⁻¹ βˆ’ 1

We want to prove:

T(nβˆ’1) = 2^(nβˆ’1) βˆ’ 1

Which means: moving (nβˆ’1) disks takes that many moves.

βœ… Base Case: n = 1

(nβˆ’1) = 0 disks β†’ takes 0 moves

2^0 βˆ’ 1 = 1 βˆ’ 1 = 0 βœ…

βœ… Case n = 2

(nβˆ’1) = 1 disk β†’ takes 1 move

2^1 βˆ’ 1 = 2 βˆ’ 1 = 1 βœ…

So to move 2 disks:

– Move 1 to helper β†’ 1 move

– Move largest to target β†’ 1 move

– Move 1 to target again β†’ 1 move

Total: 1 + 1 + 1 = 3 βœ…

πŸ” Inductive Step:

Assume T(nβˆ’2) = 2^(nβˆ’2) βˆ’ 1 (true for smaller cases)

Then T(nβˆ’1) =

Β Β = 2 Γ— T(nβˆ’2) + 1

Β Β = 2 Γ— (2^(nβˆ’2) βˆ’ 1) + 1

Β Β = 2^(nβˆ’1) βˆ’ 2 + 1

Β Β = 2^(nβˆ’1) βˆ’ 1 βœ…

So we’ve shown that the number of moves for (nβˆ’1) disks is 2^(nβˆ’1) βˆ’ 1, just as expected.

And once we’re confident that (nβˆ’1) disks require 2^(nβˆ’1) βˆ’ 1 moves,

then moving n disks requires:

Β Β – 2 Γ— (2^(nβˆ’1) βˆ’ 1)Β  [to move nβˆ’1 disks twice]

Β Β – + 1 move for disk n itself

Which gives:

Β Β 2 Γ— (2^(nβˆ’1) βˆ’ 1) + 1 = 2^n βˆ’ 2 + 1 = 2^n βˆ’ 1 βœ…

If you read this far, nice work!

 


πŸ’¬ Final Reflection

Building this simulator with ChatGPT revealed something profound:
Recursion isn’t just a computer science trick. It’s a way of thinking. A way of trusting that if you solve the smallest pieces with care, the larger problem will resolve itself β€” naturally, and beautifully.

#DesignThinking #ComputationalThinking #GoogleAppsScript #EdTech #MYPDesign #TeachingInnovation #TowerOfHanoi #Recursion #AIinEducation