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[{n=start,seq={start}},
Do[
(* compute the next value and append to the sequence *)
n=func[n];
AppendTo[seq,n],

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

(* return the sequence *)
seq
]

Iterate the ColVar function on some starting values. 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

findCycle[func_,start_,maxIter_:10000]:=Module[
{seq={start} (* a list to store the sequence that we compute *),
n=start (* the current value *),
p  (* position of a repeated value, if we find a repeated value *)
},

(* main loop: repeat until we find a repeated value or maxIter iterations*)
Do[
(* compute the next iterate *)


(* check if we have seen this value before, if so, return the cycle *)


(* otherwise, append the value to the sequence *)


,maxIter];

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