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:
- Move nβ1 disks from source to auxiliary
- Move the nth (largest) disk to target
- 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:
- Move 1 disks from Source to Helper β T(1) moves
- Move disk 2 to Target β 1 move
- 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:
- Move 2 disks from Source to Helper β T(2) moves
- Move disk 3 to Target β 1 move
- 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:
- Move 3 disks from Source to Helper β T(3) moves
- Move disk 4 to Target β 1 move
- 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:
- Move 4 disks from Source to Helper β T(4) moves
- Move disk 5 to Target β 1 move
- 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:
- Move 5 disks from Source to Helper β T(5) moves
- Move disk 6 to Target β 1 move
- 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:
- Move 6 disks from Source to Helper β T(6) moves
- Move disk 7 to Target β 1 move
- 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:
- Move 7 disks from Source to Helper β T(7) moves
- Move disk 8 to Target β 1 move
- 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:
- Move 8 disks from Source to Helper β T(8) moves
- Move disk 9 to Target β 1 move
- 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:
- Move 9 disks from Source to Helper β T(9) moves
- Move disk 10 to Target β 1 move
- 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:
- Move 10 disks from Source to Helper β T(10) moves
- Move disk 11 to Target β 1 move
- 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