Visualization of the Collatz Conjecture by Edmund Harris
The Collatz Conjecture
"Mathematics may not be ready for such problems." - Paul Erdos
Introduction
The Collatz Conjecture, also known as the 3x+1 problem, is a fascinating mathematical puzzle that has been named after the German mathematician Lothar Collatz. This conjecture arises from an iterative process where you start with any positive integer and alternate between two simple rules:
if the number is even, you divide it by 2,
and if it's odd, you multiply it by 3 and add 1.
For example, take the number 3. It's odd, so we multiply by 3 and add 1. We get 10. Now that's even, so we can divide it by 2, to get 5. Back to odd, so let's multiply that by 3 and add 1. We are now at 16, which is very even. So much so that we can keep on dividing by 2 until we reach 1.
$3 \rightarrow 10 \rightarrow 5 \rightarrow 16 \rightarrow 8 \rightarrow 4 \rightarrow 2 \rightarrow 1$
What happens when we reach 1? Well, it's odd so we multiply by 3 and add 1. And we are back at 4, which leads back to one. We have reached a cycle.
$4 \rightarrow 2 \rightarrow 1 \rightarrow 4 \rightarrow 2 \rightarrow 1 \ldots$
The question is, can you predict what the number will be after a certain number of iterations?
The conjecture is that no matter what starting number you choose, regardless of its size, you will always reach the number 1.
However, despite being relatively simple to understand and easy to test for small numbers, it has so far proven difficult to prove definitively for all cases. This conjecture is an unsolved problem in mathematics that continues to intrigue both mathematicians and enthusiasts alike.
The Hailstone Sequence
The sequence of values that you go through when iterating a number is often called the hailstone sequence, as the numbers go up and down through the sequence.
Starting value =
All Paths Lead to One
We can visualize the path that every number takes by overlaying many hailstone sequences at once. Each line is one starting value; watch how, no matter where they begin, they all tumble down into the same $4 \rightarrow 2 \rightarrow 1$ cycle.
Overlay paths for 1 β¦ N =
The stopping time of a number
At first it might seem that the fact that it always reaches 1 could appear strange, as some numbers get caught in a repeating pattern of multiplying by 3 and adding one, when dividing by 2, give a another odd number. Since:
$\begin{aligned} x < \frac{3x + 1}{2} \end{aligned}$
Thus, it's possible (and quite frequent) that we end going up in numbers, and looks like we are getting further away from the pit of doom that is the number 1.
However, this is unfortunately not the case, but we quantify this by calculating how long it takes for a number to reach a another number that is lower than the starting point: the stopping time.
Here is a plot to show the total stopping times of the numbers for up to 1000.
Upper bound =
Interactive Visualization
The interactive visualization above runs live in your browser as a WebAssembly island β drag the sliders to explore. (Exporting the rendered image to a standalone HTML file is only available when running this notebook in a full Pluto session.)
The window size controls feed the interactive visualization below.
Of course, we are not limited to the 3x + 1 problem, what happens if we change up those values?
Generalizing the Collatz function
A generalization of the collatz function is the following:
$g(n) = n/P \ \ \ \ \ \ \ \text{when}\ \ \ n \ \text{mod}\ P = 0$
$g(n) = an+b \ \ \ \text{otherwise}$
This formulation makes sure that we always deal with integers.
The sliders below let you change the three numbers $P$, $a$ and $b$ directly. Every plot above β the hailstone sequence, the convergence view and the stopping-time plot β reacts live to these values, so you can explore the generalized family without leaving the page.
Try it
The classic Collatz problem is $P = 2$, $a = 3$, $b = 1$. Move the sliders to pick a different $(P, a, b)$ and watch how the trajectories change.
Collatz Parameters (P, a, b):
P =
a =
b =
Divergence
Some parameter choices do not behave like the traditional problem and can send the numbers climbing without ever reaching 1. To keep everything fast and finite, the computations stop after at most 1000 steps.
Gallery
While playing around with the interactive visualization above, you can stumble into some lovely patterns. Try a few (P, a, b) combinations together with different rotation angles, step lengths and starting points β for example P = 5, a = 5, b = 5 or P = 2, a = 3, b = 7 β and watch the trajectories bloom into flowers, hexagonal grids and little creatures. Enjoy :)
(Note that the patterns are highly dependent on the canvas size, so the exact look will shift as you resize the window.)
Appendix
Here a list of extra ressources in case you want to learn more. They inspired me a lot through this notebook so hope you find them usefull!
[This amazing post from Luc Blassel] (https://lucblassel.com/posts/visualizing-the-collatz-conjecture/)
Packages
Numerical kernels
The Collatz / hailstone / stopping-time computations in this notebook are written as plain integer loops (collatz_hailstone, collatz_stopping_time) so the interactive plots compile to WebAssembly β no external numerical packages needed.
Notebook Packages
PlutoUI: Extension for Pluto to handle interactivity, provides the Sliders and Checkboxes.
HypertextLiteral: Used to lay out the interactive control panels.
Plotting Package
WasmMakie: Plotting library for the several plots and trajectory visualizations in the notebook β Makie's API, rendered through HTML Canvas2D, WebAssembly-compilable.
begin
using WasmMakie
md"""
!!! info "Plotting Package"
[WasmMakie](https://github.com/GroupTherapyOrg/WasmMakie.jl): Plotting library for the several plots and trajectory visualizations in the notebook β Makie's API, rendered through HTML Canvas2D, WebAssembly-compilable.
"""
endCustom Types
Functions
Collatz
collatz_hailstone(n0, P, a, b, maxlen)
The generalized hailstone sequence as a flat Vector{Int64}, computed with a plain integer loop so it compiles to WebAssembly. g(n) = n Γ· P when n % P == 0, otherwise g(n) = a*n + b. Iteration stops at 1 or after maxlen steps.
collatz_stopping_time(n0, P, a, b, maxlen)
Number of steps the generalized Collatz map takes to first reach 1 (the total stopping time), as a plain Int64. Capped at maxlen steps so divergent parameters stay finite.
Drawing
HTML Functions
Interactivity extensions
All interactive controls now use plain PlutoUI bonds (@bind x Slider(...)), which serialize correctly as WebAssembly island bonds.
(NumberField controls use plain @bind x NumberField(...) bonds.)
(CheckBox controls use plain @bind x CheckBox(...) bonds.)
(Stroke colour now uses three integer @bind x Slider(0:255, ...) channel bonds.)
(slider layout is built inline in each control cell.)
(slider layout is built inline in each control cell.)
(NumberField layout is built inline in each control cell.)
(CheckBox layout is built inline in each control cell.)
(Stroke colour slider layout is built inline in the control cell.)