Exploring a Variant of the Collatz Function

Math 242 Modern Computational Mathematics

Define a variant of the Collatz function

Let’s modify the Collatz function by changing 3n+1 to 3n-1.

colVar[n_?OddQ]:=3n-1
colVar[n_?EvenQ]:=n/2

Here is a modified version of our makeSeq module from a week ago. This version takes three inputs: a function, a starting value, and a number of iterations; it returns a sequence of iterates.

makeSeq[func_,start_,numIter_]:=Module[{curr=start,seq={start}},
Do[
(* compute the next value and append to the sequence *)
curr=func[curr];
AppendTo[seq,curr],

{numIter} (* repeat numIter times *)
];

(* return the sequence *)
seq
]

Compute a trajectory of my colVar function:

makeSeq[colVar,1,20]

Let's iterate a different function:

f[x_]:=x^2

makeSeq[f,2,8]

What cycles arise when we iterate colVar?

Table[makeSeq[colVar,n,25],{n,1,20}]

FindCycle module

findCycle[func_,n_]:=Module[{traj={n},last,next,j},
Do[
last=Last[traj];
next=func[last];
If[MemberQ[traj,next],Break[],AppendTo[traj,next]]
,100];

j=Position[traj,next][[1,1]];
traj[[j;;]]
]

findCycle[colVar,3]

makeSeq[colVar,3,20]

Examples of using MemberQ to determine if a value is in a list:

MemberQ[{2,5,7,4},5]

MemberQ[{2,5,7,4},6]

Find some cycles!

Table[findCycle[colVar,n],{n,1,100}]

Tally[%]

Table[Length[findCycle[colVar,n]],{n,1,100}]

Tally[%]