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
]

makeSeq[colVar,1,20]

Unrelated example:

f[x_]:=x^2

makeSeq[f,2,8]

What cycles appear when iterating the colVar function?

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

Table[makeSeq[colVar,n,50],{n,{5,7,9,10}}]

MemberQ[{1,2,3},2]

MemberQ[{1,2,3},5]

Position[{6,2,5,87,2},5][[1,1]]

alist={4,7,2,5,456};
alist[[2;;]]

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

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

findCycle[colVar,3]

makeSeq[colVar,3,20]

makeSeq[colVar,17,40]

findCycle[colVar,17]

Make a list of cycles for lots of starting values:

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

Tally[%]