The Collatz Function

Math 242 Modern Computational Math, spring 2021

Define the Collatz function:

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

Try it out for some input values:

c/@Range[10]

Iterate the Collatz function for various starting values n. What do you observe?

Useful function: NestList[function, start, number of iterates]

NestList[c,5,20]

NestList[c,47,500]

NestList[c,3331,200]

Let' s Produce a sequence of iterates until we reach 1

colSeq[n_]:=Module[{seq={n}},
While[Last[seq]≠ 1,
AppendTo[seq,c[Last[seq]]]
];
seq   (* return the sequence *)
]

Try it out:

colSeq[5]

colSeq[3331]

ListPlot[colSeq[3331],PlotRange->All]

ListPlot[colSeq[3823],PlotRange->All]

Length of a Collatz Sequence

colLen[n_]:=Length[colSeq[n]]

ListPlot[colLen/@Range[10000],AxesLabel->{"n","length"}]