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.

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

col/@Range[10]

Iterate a fixed number of times

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

n=5;
numIter=10;
sequence={n};
Do[
n=col[n];
AppendTo[sequence,n],
numIter
]
sequence

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

makeSeq[start_,numIter_]:=Module[
(* local variables *) 
{n=start,sequence={start}},

(* main loop *)
Do[
n=col[n];
AppendTo[sequence,n],
numIter
];

(* return value *) 
sequence
]

makeSeq[17,40]

makeSeq[128,30]

makeSeq[27,200]

ListPlot[makeSeq[27,150],Joined->True,PlotRange->All,AxesLabel->{"iteration","value"}]

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.)

Iterate until we find a 1

Collatz trajectory: sequence of Collatz iterates starting with n until reaching 1

computeTrajectory[start_]:=Module[
(* local variables *) 
{n=start,sequence={start}},

(* main loop *)
While[n!=1,
n=col[n];
AppendTo[sequence,n]
];

(* return value *) 
sequence
]

computeTrajectory[27]

Height: the maximum value in the Collatz trajectory

height[n_]:=Max[computeTrajectory[n]]

height[27]

heightList=height/@Range[2000];

Position[heightList,9232]

computeTrajectory[31]

Tally[heightList]

ListPlot[heightList,PlotHighlighting->None]