The Collatz Function

Math 242 Modern Computational Math, spring 2021

Define the Collatz function:

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

c/@Range[10]

To iterate a function, we can use NestList[function, starting value, number of iterates]:

NestList[c,5,20]

Your turn: Try some other starting values. What patterns do you find?

NestList[c,27,300]

NestList[c,1024,20]

Produce a Collatz Sequence until we get 1

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

colSeq[27]