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.
In this notebook the so-called "shortcut" version of the Collatz function is used:
f(n)=
n
2
n≡0(2mod)
3n+1
2
n≡1(2mod)
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]).
Remark: Consider following the warnings in [Vv1] and elsewhere:
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

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

Collatz sequence numbers

Here is the simplest, informative Collatz sequence -- or hailstone numbers -- plot:
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[]=
185
249
270
278
370
374
492
498
540
562
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

Here is a dynamic interface to plotting the Collatz (hailstone) sequences:
Out[]=
​
seed:
395634
Length:
99, Max value:
1069442

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
Min
1
1st Qu
25000.5
Mean
50000.5
Median
50000.5
3rd Qu
75000.5
Max
100000
,
2 length
Min
1
1st Qu
47
Median
68
Mean
72.8895
3rd Qu
97
Max
222
,
3 max
Min
1
1st Qu
42272
Median
85292
3rd Qu
162980
Mean
320578.
Max
785412368

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

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

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.
Remark: SunflowerEmbeddingPlot is provided by the paclet "NumberTheoryUtiltiies", [AAp4].

Small graphs

Define a sub for graph-edge relationship between consecutive integers in Collatz sequences:
​
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

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

Articles, blog posts

◼
  • [KJR1] KJ Runia, "The Collatz Conjecture" , (2020), OpenCurve.info .
  • ◼
  • [PZ1] Parker Ziegler "Playing with the Collatz Conjecture" , (2021), ObservableHQ .
  • ◼
  • [Wk1] Wikipedia entry, "Collatz conjecture" .
  • ◼
  • [WMW1] Wolfram Math World entry, "Collatz Problem" .
  • Notebooks

    ◼
  • [IFn1] Ian Ford, "Exploring the Collatz Problem Tree", (2024), Wolfram Community.
  • ◼
  • [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

    ◼
  • [AAp1] Anton Antonov, Graph Raku package , (2024-2025), GitHub/antononcube .
  • ◼
  • [AAp2] Anton Antonov, JavaScript::D3 Raku package , (2022-2025), GitHub/antononcube .
  • ◼
  • [AAp3] Anton Antonov, Math::NumberTheory Raku package , (2025), GitHub/antononcube .
  • ◼
  • [AAp4] Anton Antonov, NumberTheoryUtilities Wolfram Language paclet, (2025), Wolfram Language Paclet Repository.
  • ◼
  • [EZf1] Enrique Zeleny, Collatz, (2019-2021), Wolfram Function Repository.
  • Videos

    ◼
  • [Vv1] Veritasium, "The Simplest Math Problem No One Can Solve - Collatz Conjecture" , (2021), YouTube@Veritasium .
  • CITE THIS NOTEBOOK

    Collatz conjecture visualizations​
    by Anton Antonov​
    Wolfram Community, STAFF PICKS, May 27, 2025
    ​https://community.wolfram.com/groups/-/m/t/3467978