Exploring a Variant of the Collatz Function

MATH 242 Modern Computational Mathematics

Define a new function

Let’s modify the Collatz function by changing 3n+1 to 3n-1. That is, define ColVar(n)=3n-1 if n is odd, and ColVar(n)=(n/2) if n is even.

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[
(* initialize variables *)
{curr=start,seq={start}},

(* main loop *)
Do[
(* compute the next value and append to the sequence *)
curr=func[curr];
AppendTo[seq,curr],

numIter (* repeat numIter times *)
];

(* return the sequence *)
seq
]

Iterate the ColVar function on some starting values. Keep track of the sequences you obtain.

makeSeq[colVar,5,30]

TableForm[Table[makeSeq[colVar,n,30],{n,1,20}],TableSpacing->{1,1}]

How to find a repeated value in a trajectory?

testList=makeSeq[colVar,18,30]

MemberQ[testList,7]

MemberQ[testList,4]

Position[testList,7]

Position[testList,4]

What do you observe? What do you wonder?

Write a module that detects when a trajectory repeats a value

Let's write a module that finds cycles. This is a version of Algorithm 3.12 in the text. Our version takes three parameters:

A function to iterate

A starting value

Optionally, a maximum number of iterations

If this module finds a cycle, then it will return that cycle as a list. If no cycle is found, then the module will return False.

findCycle[func_,start_,maxIter_:10000]:=Module[
(* initialize variables *) 
{traj={start} (* a list to store the trajectory that we compute *),
next,pos
},

(* main loop: repeat until we find a repeated value or maxIter iterations*)
Do[
(* compute the next iterate *)
next=func[ traj[[-1]] ];

(* check if we have seen this value before, if so, return the cycle *)
If[ MemberQ[traj,next],
(* if true, return the cycle *)
pos=Position[traj,next][[1,1]];
Return[traj[[pos;;]],Module],
(* otherwise, append the value to the sequence *)
AppendTo[traj,next]
]
,maxIter];

(* if we get here, then we didn't find a repeated value *)
False
]

Test our module by comparing its output with that of makeSeq:

findCycle[colVar,26,100]

makeSeq[colVar,26,100]

findCycle[colVar,42,100]

makeSeq[colVar,42,100]

Questions to Investigate

What cycles occur in ColVar trajectories?

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

Example: which ColVar trajectories  contain the number 7?

results=Table[findCycle[colVar,n],{n,1,100}];

Table[MemberQ[traj,7],{traj,results}]

How often does each cycle occur?

What starting values lead to each cycle?

What patterns do you find in heights of ColVar trajectories?

What patterns do you find in stopping times of ColVar trajectories?

What other questions do you have?