The Collatz Function

MATH 242 Modern Computational Mathematics

Define the Collatz function

The Collatz function Col(n) is defined for any positive integer n as follows: if n is even, then Col(n)=(n/2), but if n is odd, then Col(n)=3n+1.

Iterate a fixed number of times

Let's write some code to iterate the Collatz function from some starting integer n.

n=11;(* starting value *)
numIter=20; (* number of iterations *)
seq={n}; (* initial list *)

(* main loop *) 
Do[

]
seq

We can put our code in a module to make it easy to run for different starting values:

makeSeq[start_,numIter_]:=Module[
{n=start,seq={start}},



]

Iterate until we find a 1

Let's now write a module that iterates the Collatz function from some starting integer n, stopping when we obtain the value 1. Since we don’t have any guarantee of reaching 1, we should ensure that the module stops after some maximum number of iterations. (We want to avoid an infinite loop.) This corresponds to Algorithm 3.4 in the text.

computeTrajectory[start_,maxIter_:100000]:=Module[
{n=start,traj={start},counter=0},
While[


];
traj
]

Plotting trajectories

What does a Collatz trajectory look like? Here is a nice way to visualize trajectories.

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

Asking good mathematical questions

Asking good mathematical questions is not only an art, but also an important skill to develop. A good mathematical question:

Is interesting to you.

Is not an question whose answer is known to you.

Is not a question you've seen before, or at least not exactly.

Is a question about which you can begin to make computational investigations.

(These four criteria are from Barry Mazur and William Stein, in Prime Numbers and the Riemann Hypothesis.)