↑ ↓ to move · enter to open

005 · Self-similarity · Recursion

recursion & fractals

A recursive solution solves a problem by solving a smaller copy of itself. Draw that idea instead of running it and you get a fractal: a shape made of smaller copies of itself, all the way down.

LAB 1

the tower of hanoi

Move the whole tower from the left peg to the right one. Move one disk at a time and never put a bigger disk on a smaller one. To move n disks: move the top n − 1 out of the way, move the biggest disk, then move the n − 1 back on top. That's the entire solution.

auto
5
Recursion tree · read left to right = move order

How to read this: on the pegs, disk color only tracks size. In the tree below them, each dot is one move. Higher dots move bigger disks, and every dot sits between the two smaller jobs it waits on.

  • move happening now
  • moves done
  • moves still to come

Try this: switch to Solve it yourself with 3 disks (the gold peg is the one you picked), then compare your moves with the gold Fewest possible count.

0
Moves made
31
Fewest possible · 2ⁿ − 1

Now

Call stackdepth 0

Each line is a job waiting on the one below it; the gold line is moving a disk right now. Depth counts the waiting jobs.

The code

def hanoi(n, src, dst, spare):
  if n == 0: return
  hanoi(n-1, src, spare, dst)  # clear the way
  move(n, src, dst)            # biggest disk
  hanoi(n-1, spare, dst, src)  # restack on top
T(n) = 2·T(n−1) + 1 = 2ⁿ − 1

Each extra disk doubles the work. The legend's 64 golden disks, at one move a second, would take about 585 billion years.

LAB 2

shapes made of themselves

Each shape is one short recursive rule applied over and over. Raise the depth and watch it draw in the order the recursion visits each piece.

Koch snowflake · depth 4
4
  • the piece being drawn right now
  • tree: trunk, fading to green at the deepest twigs

Try this: Fractal tree at depth 10, then drag Angle.

The rule


        

What it shows

LAB 3

the mandelbrot set

Pick a point c on the complex plane. Start at z = 0 and repeat z → z² + c. If z stays close to the origin forever, c is in the set (black). Colors show how fast the other points fly away. Click to zoom in ×3, and hover to watch a point's path.

Hover the image to trace a point's orbit.
Go to

How to read this: hover a point: the gold ring is the point you picked, and the dots show where repeating the rule takes it. The colored bands repeat; each band is one more step before the point escapes, so they don't run from fast to slow.

  • black: in the set
  • trail flies off: not in the set
  • trail stays close: in the set

Try this: click Seahorse valley, then keep clicking a curl. Right-click or Shift-click zooms back out.

1×
Magnification
200
Max iterations
−0.5 + 0i
Center

Why |z| > 2 means gone

Once |z| passes 2, squaring it grows faster than adding c can pull it back, so it escapes to infinity. The renderer stops at that point and colors the pixel by how many steps it took.

z₀ = 0, zk+1 = zk² + c, c ∈ M ⇔ |zk| ≤ 2 for all k

Where the recursion hides

Zoom into any bulb's edge and you find smaller copies of the whole set, each with its own bulbs and its own copies. The boundary is so crinkled that its fractal dimension is exactly 2, as Shishikura proved in 1998. The area is still unknown, but it's estimated at about 1.5066.