Collatz Investigation

Define the Collatz function:

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

Try it out:

c/@Range[10]

We can iterate the Collatz function using NestList:

NestList[c,5,20] (* return 20 iterates of c(n), starting at n=5 *)

NestList[c,33,100]

Produce a sequence of iterates until we reach 1

Write a module that takes a starting value n and returns a sequence of iterates until it reaches 1:

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

Try it out:

s=colSeq[27]

ListPlot[s,PlotRange->All]

Length of the Collatz Sequence

Find the length of the sequence of iterates starting at n:

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

colLen[33]

Compute the lengths of many Collatz sequences!

lenList=colLen/@Range[10000]

Make a plot:

ListPlot[lenList]