Collatz Investigation

Define the Collatz function:

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

Try it out:

c/@ Range[10]

Iterate this function for various starting values n. One way is to use NestList:

NestList[c,5,30]

NestList[c,13,30]

NestList[c,27,150]

Produce a sequence of iterates until we find 1:

Write a module to produce the sequence of iterates from n to 1.

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

Try it out:

colSeq[27]

Plot the sequence:

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

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

Length of the sequence

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

colLen[27]

Plot the length of the sequence for a bunch of n:

ListPlot[ colLen /@ Range[10000] ]