Introduction
Introduction
This notebook presents various visualizations related to the Collatz conjecture , [WMW1, Wk1] using Wolfram Language (WL).
The Collatz conjecture, a renowned, unsolved mathematical problem, questions whether iteratively applying two basic arithmetic operations will lead every positive integer to ultimately reach the value of 1.
f(n)=
|
That function is used repeatedly to form a sequence, beginning with any positive integer, and taking the result of each step as the input for the next.
The Collatz conjecture is: This process will eventually reach the number 1, regardless of which positive integer is chosen initially.
WL-wise, functions for the Collatz sequences are easy to define. There are both built-in, [VKn1], and resource functions, [EZf1], for computing Collatz sequences.
There are many articles, blog posts, and videos dedicated to visualizations of the Collatz conjecture. (For example, [KJR1, PZ1, Vv1] and the Wolfram Community posts [IFn1, KSn1, VKn1]).
Do not work on this [Collatz] problem! (Do real math instead.)
Remark: In my opinion, this notebook provides both basic and advanced (neat looking) plots that paint a better introductory “picture” of the Collatz conjecture.
Collatz function definition
Collatz function definition
Here is a function for the shortcut version of the Collatz function:
In[]:=
Clear[Collatz]Collatz[n_Integer?Positive]:=NestWhileList[If[EvenQ[#],Quotient[#,2],(3#+1)/2]&,n,#1≠1&];
Here is an example using 26 as a sequence seed (i.e. starting value):
In[]:=
Collatz[26]
Out[]=
{26,13,20,10,5,8,4,2,1}
The next integer, 27 , produces a much longer sequence:
In[]:=
Collatz[27]//Length
Out[]=
71
Simple visualizations
Simple visualizations
Collatz sequence numbers
Collatz sequence numbers
In[]:=
opts={PlotRange->All,PlotTheme->"Detailed",ImageSize->Large};ListLinePlot[Collatz[171],PlotLabel->"Hailstone numbers of 171",opts]
Out[]=
Let us make a multi-lines plot for a selection of integers:
In[]:=
SeedRandom[8893];data=With[{seqs=Select[Table[Collatz[n],{n,1,1000}],30<=Length[#]<=150&&Max[#]<=600&]},SortBy[RandomSample[seqs,UpTo[10]],First]//Map[With[{i=First[#]},MapIndexed[<|"group"->i,"x"->#2[[1]],"y"->#1|>&,#]]&]//Flatten[#,1]&];aData=GroupBy[data,#group&,Map[{#x,#y}&,#]&];ListLinePlot[Values@aData,PlotLegends->Keys[aData],opts]
Out[]=
Remark: Using simple sampling like the code block below would generally produce very non-uniform length- and max-value sequences. Hence, we do the filtering above.
data = Flatten[Map[Function[i, With[{seq = Collatz[i]}, MapIndexed[<|"group" -> i, "x" -> First@#2, "y" -> #1|> &, seq]]], Sort[RandomSample[Range[0, 99], 9]]], 1]
Dynamic interface
Dynamic interface
Here is a dynamic interface to plotting the Collatz (hailstone) sequences:
Out[]=
Distributions of sequence length and max values
Distributions of sequence length and max values
Here are Collatz sequences and their corresponding lengths and max-values:
In[]:=
AbsoluteTiming[m=100000;cSequences=Table[Collatz[n],{n,1,m}];cLengths=Length/@cSequences;cMaxes=Max/@cSequences;dsCollatz=Transpose[{Range[1,m],cLengths,cMaxes}];dsCollatz=Dataset@Map[AssociationThread[{"seed","length","max"},#]&,dsCollatz];]
Out[]=
{6.803,Null}
In[]:=
ResourceFunction["RecordsSummary"][dsCollatz]
Out[]=
,
,
1 seed | ||||||||||||
|
2 length | ||||||||||||
|
3 max | ||||||||||||
|
Here are histograms of the Collatz sequences lengths and max-value distributions:
In[]:=
Row[{Histogram[cLengths,PlotLabel->"Collatz sequences lengths distribution (up to "<>ToString[m]<>")",opts],Spacer[20],Histogram[Log10@cMaxes,40,PlotLabel->"Collatz sequences lg(max-value) distribution (up to "<>ToString[m]<>")",opts]}]
Out[]=
Here is a scatter plot of seed vs. sequence length:
Benford's law adherence
Benford's law adherence
It is of interest to see the manifestation of Benford's law for the first digits of Collatz hailstones. Here is the corresponding digit tally:
Here is a comparison with the corresponding Benford's law values:
Good adherence is observed for a relatively modest number of sequences. Here is a corresponding bar chart:
Here is the corresponding correlation:
Sunflower embedding
Sunflower embedding
A certain concentric pattern emerges in the spiral embedding plots of the Collatz sequences lengths modulo 8. (Using in addition modulo 3 makes the pattern clearer.) Similarly, a clear spiral pattern is seen for the maximum values.
Small graphs
Small graphs
For didactic purposes let use derive the edges of a graph using a certain small number of iterations:
For didactic purposes let use derive the edges of a graph using a certain small number of iterations:
Make the graph:
Plot the graph using suitable embedding:
The Collatz sequence paths can be easily followed in the tree graph.
Big graph
Big graph
Let us make a bigger, visually compelling graph:
Next we find the path lengths from the root to each vertex in order to do some sort concentric coloring:
Here is the graph plot:
References
References
Articles, blog posts
Articles, blog posts
◼
◼
◼
◼
Notebooks
Notebooks
◼
◼
[KSn1] Keith Stendall, "The Collatz conjecture: An investigation using infinite integer sequences.”, (2023), Wolfram Community.
◼
[VKn1] Vitaliy Kaurov, "A beautiful picture related to Collatz conjecture", (2015), Wolfram Community.
Functions, packages, paclets
Functions, packages, paclets
◼
◼
◼
◼
[AAp4] Anton Antonov, NumberTheoryUtilities Wolfram Language paclet, (2025), Wolfram Language Paclet Repository.
◼
Videos
Videos
◼
[Vv1] Veritasium, "The Simplest Math Problem No One Can Solve - Collatz Conjecture" , (2021), YouTube@Veritasium .
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Collatz conjecture visualizations
by Anton Antonov
Wolfram Community, STAFF PICKS, May 27, 2025
https://community.wolfram.com/groups/-/m/t/3467978
by Anton Antonov
Wolfram Community, STAFF PICKS, May 27, 2025
https://community.wolfram.com/groups/-/m/t/3467978

