Heights of Collatz Trajectories

Math 242 Modern Computational Mathematics

Preliminary Code

Here is our basic code for defining the Collatz function and computing trajectories.

col[n_?OddQ]:=3n+1
col[n_?EvenQ]:=n/2
computeTrajectory[start_,maxIter_:100000]:=Module[{traj={start},n=start},
(* compute the trajectory until we find 1 *)
While[n!=1&&Length[traj]<maxIter,
n=col[n];
AppendTo[traj,n];
];
(* return the trajectory *)
traj
]

computeTrajectory[27]

computeTrajectory[27]//Length

Stopping time of Collatz trajectories

Recall from the text that stop(n) is the number of iterations of the function col required to reach 1 from n.

stop[n_]:=Length[computeTrajectory[n]]-1

computeTrajectory[5]

stop[5]

stop[27]

Compute a list of stopping times:

stopList=stop/@Range[50]

ListPlot[stopList,AxesLabel->{"n","stop(n)"},PlotRange->All]

stopList=stop/@Range[1000];

ListPlot[stopList,AxesLabel->{"n","stop(n)"},PlotRange->All]

Why are there horizontal segments in the plot?

Position[stopList,18]

We see that if n=28, 29, or 30, then the stopping time is 18.

computeTrajectory/@{28,29,30}

How do we find consecutive identical values in a list?

Define a streak to be multiple consecutive identical values in a list.

stopList

Streak detection module

detectStreak[list_,desiredLen_]:=Module[{pos=2,currStreak=1},

(* loop over list entries until we find a streak *)
While[currStreak<desiredLen && pos<=Length[list],
If[list[[pos]]==list[[pos-1]],currStreak+=1,currStreak=1];
pos+=1
];

(* return value *)
If[currStreak==desiredLen,pos-currStreak,False]
]

test it out:

detectStreak[{5,7,2,2,2},3]

detectStreak[{5,7,2,2,2},4]

detectStreak[{5,7,2,2,3,2,2,2,5},3]

Are there 5 consecutive integers with the same stopping time? If so, what are they?

detectStreak[stopList,5]

stop[98]

computeTrajectory[98]

stop/@Range[98,102]

detectStreak[stopList,97]