0
$\begingroup$

This question already has an answer here:

By definition of Collatz Conjecture or $3n+1$, regardless of the sequence (ie.$ m1,m2,m3....mn$), at the end, it eventually produces 1 at the end. For example, if you let $m=10$ (ie. $10, 5, 16, 8, 4, 2, 1, 4, 2, 1...$), you must repeat 6 times and eventually reach to 1.

How can Module

Collatz[m_]:= Module[{...}]  

be used that takes positive integer $m$ and outputs the "number of times" that the procedure must be repeated until obtaining 1?

Side Note: Although the link does incorporate the Module function, It's not about longest [Collatz] chain.

$\endgroup$

marked as duplicate by corey979, Henrik Schumacher, Patrick Stevens, Daniel Lichtblau, José Antonio Díaz Navas Apr 3 '18 at 9:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    $\begingroup$ It can be done as a straightforward loop. Not necessarily the best way, but reasonably effective. Collatz[m_] := Module[{j = 0, m1 = m}, While[m1 =!= 1, j++; m1 = If[OddQ[m1], 3*m1 + 1, m1/2]]; j] $\endgroup$ – Daniel Lichtblau Apr 2 '18 at 22:11
5
$\begingroup$

Recursion comes in handy here:

Collatz[m_] := Collatz[m] = 1 + If[EvenQ[m], Collatz[m/2], Collatz[3 m + 1]];
Collatz[1] := 0;
Collatz[10] 
(* 6 *)
$\endgroup$
  • 1
    $\begingroup$ +1 Recommend that definition start off as Collatz[m_Integer?Positive] :=... $\endgroup$ – Bob Hanlon Apr 3 '18 at 1:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.