# conditional: how do I modify my code to detect cycles in this recursion type Collatz?

A popular variant of Collatz's recursion is as follows. Given a positive integer $$n_i$$,

• Yes $$n_i$$ even, $$n_ {i + 1} = 2n$$,
• Yes $$n_i$$ it's odd $$n_ {n + 1} = 3n-1$$.

The Mathematica code that I am using to display the list of $$n_i$$ For a given initial number it is:

collatz[x_] :=  NestWhileList[If[Mod[#, 2] == 0, #/2, #*3 - 1] &, x, # != 1 &]

However, this variant of the Collatz has cycles other than the trivial one. $${4,2,1 }$$, for example for initial values $$5$$ Y $$17$$. The above code only stops once $$1$$ it is reached, which obviously creates an infinite loop for these inputs.

How do I modify the previous code so that other "loops" are also taken into account? I tried to modify the detention condition to include something like # != x But that condition is obviously met at the beginning of the cycle, and the introduction of variables that verify if it is the "first" iteration makes the code very messy.

Is there any "nice" way to do this?