Visualizations
403 / 500

403 · Generative art

Collatz Coral

Thousands of Collatz sequences grow backward from 1 into a swaying coral.

The Collatz map halves even numbers and sends odd n to 3n + 1, and every number anyone has tried eventually reaches 1, though nobody has proved that all of them do. The sequences from every start up to a few thousand are merged into one tree rooted at 1 and drawn backward: each step turns one way when it lands on an even number (a halving) and the other way when it lands on an odd one (a 3n + 1 step), so long runs of halvings curl and odd steps kink the branch back. Branch width follows how many starting numbers flow through it, so the trunk carries every sequence and the tips carry one, and the whole coral is turned so its mass rises from the root. A slow wave of extra turning travels out along every branch and accumulates toward the tips, so it sways like coral in a current, over sunbeams, caustics rendered in a small buffer, and distant copies of itself lost in the blue.

Try it. Hover (or tap) any branch to trace its sequence back to 1 and see its number, how many steps it takes and how high it climbs. Drag the Even turn, Odd turn and Thickness sliders (or use the arrow keys and the bracket keys) to reshape it, switch between 2,000, 6,000 and 15,000 sequences with the count button or C, and press New coral or Space to glide to another shape.

  • Collatz sequence tree
  • Accumulated angle turtle drawing
  • Width by subtree weight

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build a Collatz conjecture coral with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Paint a soft sea-blue gradient background.
- For every starting number from 2 to about 3,000, compute its Collatz sequence (n / 2 if n is even, 3n + 1 if odd) down to 1.
- Draw each sequence backward, starting at 1: begin at the bottom center of the screen heading straight up, and for each number on the way back out, turn left by a small angle (about 8 degrees) if it is even and right by a larger one (about 16 degrees) if it is odd, then step forward a fixed length.
- Sequences share their endings, so store the numbers you have already placed in a Map and only draw each segment once. Fit the result to the screen.

Once that works, make it beautiful:
- Make branches thicker where more sequences pass through them: count the starting numbers in each subtree and set the line width from that count.
- Color by depth, from deep coral at the trunk to pale peach at the tips, and batch segments by width and color so thousands of them draw at 60 fps.
- Make it sway: add a small turn to every segment that follows a slow sine wave traveling outward along the depth, so the tips move the most.
- Add sliders for the even and odd turn angles, and show the number and its step count when hovering near a branch.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as underwater lighting with caustics and sunbeams, rotating the coral so it grows up from the sea floor, or highlighting the whole path of a hovered number back to 1.
PreviousPlanet ForgeA dusty disk grows rocky worlds, ice giants and gas giants by N-body accretion. NextElastic Net TSPA rubber band of neurons wriggles through a map of cities until it becomes a tour.

Related visualizations

  • Differential GrowthGenerative art One closed line grows, buckles and folds into brain coral without ever crossing.
  • BiomorphsAlgorithms Dawkins' Blind Watchmaker: breed recursive ink creatures by choosing a child.
  • Turing PatternsEmergence Two chemicals react and diffuse into coral, spots and stripes.
  • KolamGenerative art A hand traces a rice-flour kolam: one unbroken line looping around every dot.
  • Fractal TreeNature A recursive tree that grows, blossoms and sways in the wind.
  • Self-Replicating LoopsEmergence Langton's 1984 loops copy their own genome and tile the plane with a glass colony.
  • Multiscale TruchetGenerative art Truchet tiles at five scales lock together seamlessly, flipping color at each level.
  • Indra's PearlsFractals Limit sets of Kleinian groups, strung as glowing necklaces of nested circles and pearls.
  • KaleidoscopeGenerative art Draw once, and twelve mirrored strokes bloom into a mandala.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs