RECURSION SIMULATION

The legend of the Tower of Hanoi puts 64 discs at 2⁶⁴−1 moves, over eighteen quintillion lifts, and yet the entire solution fits in one sentence that calls itself: move n−1 discs to the spare peg, shift the bottom disc, repeat. Every call occupies a frame on the stack and the stack is finite, so a missing base case overflows it. Naive Fibonacci branches in two at every level, so the same fib(k) is recomputed exponentially often; once results are memoized the duplicate branches are lopped off and the call tree collapses to something close to linear.

▸ Building the call stack…
▸ Loading factorial · Tower of Hanoi
▸ Loading the Fibonacci tree (± memoization)
▸ Loading recursive fractals (tree · Koch)
▸ Hashing each push/pop step for replay…
▸ Ready — Online. ✅
0%
⌂ Mind & Machine

Simulation room Recursion & Stack

Recursion & Call Stack
Online
push/pop frames · call tree · fractal
Recursion & stack
➗ Factorial n!
Total calls—
Frames / max depth—
Base hits—
Return value—
Complexity—
Viewing—
Notes
Recursion = a function that calls itself on a smaller problem, advancing toward the base case. Each call pushes a stack frame; on reaching the base, the frames pop one by one and bubble the returned value back up.
—
Pick a "Scenario" to change the recursion type (factorial · Hanoi · Fibonacci · memoized · tree · Koch) · drag n to change the depth · click a concept for details
Your browser has canvas disabled.
Total calls & stack depth over time total callsframe depth
About Recursion & Call Stack

The legend of the Tower of Hanoi puts 64 discs at 2⁶⁴−1 moves, over eighteen quintillion lifts, and yet the entire solution fits in one sentence that calls itself: move n−1 discs to the spare peg, shift the bottom disc, repeat. Every call occupies a frame on the stack and the stack is finite, so a missing base case overflows it. Naive Fibonacci branches in two at every level, so the same fib(k) is recomputed exponentially often; once results are memoized the duplicate branches are lopped off and the call tree collapses to something close to linear.

Related topics in Mind & Machine