Collatz Stopping Times

Math 242 Modern Computational Mathematics

Preliminary Code

We start with our Collatz function code from the past few class sessions. First, we define the Collatz function:

col[n_?OddQ]:=3n+1
col[n_?EvenQ]:=n/2

Next, we need our computeTrajectory module that computes the sequence of Collatz iterates from a starting integer n. This module stops when either a 1 is reached or the loop repeats maxIter times.

computeTrajectory[start_,maxIter_:100000]:=Module[
{n=start,traj={start}},
(* compute the trajectory until we find 1 *)
While[n!=1,
n=col[n];
AppendTo[traj,n];
If[Length[traj]==maxIter,Break[]] (* quit if the length of the trajectory reaches maxIter *)
];
(* return the trajectory *)
traj
]

We also want our plotTrajectory function:

plotTrajectory[trajectory_]:=ListPlot[trajectory,PlotRange->All,AxesLabel->{"iteration","value"},Joined->True,PlotStyle->Orange,Mesh->All,MeshStyle->{Blue,PointSize[Medium]}]

Stopping time of Collatz trajectories

For any positive integer n, define  stop(n) to be the number of iterations of the function col required to reach 1 from n. This is one less than the length of the list returned by our computeTrajectory function.

stop[n_]:=

Let’s compute some stopping times:

We could also use Tally to find some common stopping times:

What would a plot of stopping times look like?

What do you notice about the stopping time plot? What questions does this prompt you to ask?