# Finding the smallest starting number producing a Collatz sequence of length 125 which ends with a value > 4

I'm supposed to do a Mathematica function that selects the smallest number not less or equal to 4 after 125 iterations. I am not good at coding, so some help/advice would be awesome.

My code so far.

f[m_, n_] :=
f[m, n] = If[Divisible[f[m, n - 1], 2], f[m, n - 1]/2, 3*f[m, n - 1] + 1];
Table[f[m, i], {i, 1, 125}]


So, any tips on how to write code that lists the numbers and returns anything that gives back 4 after 125 iterations?

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Jan 19 '16 at 12:13
• You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful Jan 19 '16 at 12:15
• Please edit your question to improve it. Provide your code in formatted form, as sugested by Michael_E2. Please also explain better what is that you need, what you have, what is the problem with what you have and an example of the desired output. People will (should?) be reluctant to just give you a straight answer for your homework without you showing due diligence first. Jan 19 '16 at 12:46
• If you need to apply the same function 125 times, you may want to use Nest. I still don't understand what is that function that you need to iterate. Help us to help you, edit to explain better. Jan 19 '16 at 12:56
• It's not clear to me what role m plays in your question above. You don't appear to use it as part of your recursion.
– user1722
Jan 19 '16 at 16:24

There are many questions about the Collatz sequence on this site, for example here. I interpret your question to ask for the smallest integer n that, after 125 iterations, has not settled into the 4-2-1 loop that is conjectured to terminate all Collatz iterations. You can run 125 iterations and return the result with the following definition.

collatz125[n_Integer] := Nest[If[EvenQ[#], #/2, 3*# + 1] &, n, 125]


Make a table of integer n and its result after 125 iterations, then select from the table those values of n which are not yet in the 4-2-1 loop.

Select[Table[{n, collatz125[n]}, {n, 1, 500}], #[[2]] > 4 &]


The smallest n is 313. After 125 iterations, the result is 5.

• Very nice. Very succinct.
– user1722
Jan 19 '16 at 16:57

I decided to do this in the old-fashioned procedural way.

collatz[k_Integer?OddQ] := 3 k + 1
collatz[k_Integer?EvenQ] := k/2

collatzIter[k_ /; k > 2, n_: 125] :=
Module[{i = 0, c0 = k, c1},
While[++i < n,
c1 = collatz[c0];
If[c1 < 2, Break[]];
c0 = c1];
{i, c1}]

Module[{m = 2, r = {0, 0}},
While[r[[2]] <= 4, r = collatzIter[++m]];
{m, r}]


{231, {125, 8}}

Thus 231 is smallest number the where Collatz sequence has an element greater then 4 at position 125, and that element is 8.

<< "ExampleData/Collatz.m"
Collatz[231][[125]]


8

This of course means Collatz[231][[126]] is 4 and, therefore, fails by KennyColnago's interpretation of the OP's question, because he looks at the 126th element in the Collatz sequence.

Either answer can be easily modified to accommodate the other interpretation of what the OP is asking. The OP will have to tell us which interpretation is correct. In my case, I need only evaluate

Module[{m = 2, r = {0, 0}},
While[r[[2]] <= 4, r = collatzIter[++m, 126]];
{m, r}]


{313, {126, 5}}

to get an answer in agreement with KennyColnago's.