0

I want to calculate the longest chain up on million applying repetily the Collatz function until it reach one.

I was able to solve this using the following code:

This is the definition of the function:

 Co[n_Integer] := If[EvenQ[n], n/2, 3 n + 1];

How to calculate the lenght of the chain:

Rco[m_Integer] := Module[{n = m},
      A = {};
      While[Co[n] != 1,
       A = Join[A, {Co[n]}];
       n = Co[n];
       ];
      Join[A, {1}]
      ]

and this last piece of code that calculate the lenght of everything and take the longest

Last[SortBy[Table[{Length[Rco[n]], n}, {n, 1, 1000000}], #[[1]] &]]

This is clearly a very wasteful way to compute such a lenght. Indeed my computer took several minutes to do it.

I know that since the these chains depend at some point of previus chains I could use previus calculation to reduce the amount of work my computer has to use. But I do not know how to this.

Thanks in advance.

YTS
  • 101
  • 2

0 Answers0