I'm trying to work out a new way of visualizing the Collatz conjecture (or 3n+1 problem) using the Manipulate feature of Mathematica to show paths that numbers take in the 3n+1 problem in the form of a circle.
for those of you who don't know, the Collatz conjecture works as following, if n is even, then n is mapped to n/2, if n is odd, then n is mapped to 3n+1. The question is whether or not every number will eventually cycle into the loop [1-4-2-1]
So, when the code is working correctly, I hope that it will produce m points in a circle, and that using manipulate I will be able to choose a starting point and see that point's path around the circle. (For example, if the starting point is 3, the points path should be [3-10-5-16-8-4-2-1] Here is the current code I have
Manipulate[
u = {};
n = 0;
t = tstart;
While[1 < t < 100,
If[Mod[t, 2] == 0,
t = (t/2); n = n + 1; AppendTo[u, n],
t = 3 t + 1; n = n + 1; AppendTo[u, n]]];
Print[u];
p =
With[{nmax = Length[u]},
Table[{-Cos[N[Mod[u[[nos]], m]]*(2 Pi/m)],
Sin[N[Mod[u[[nos]], m]]*(2 Pi/m)]}, {nos, 0, nmax}]];
Print[p];
q = Table[{-Cos[nos*(2 Pi/m)], Sin[nos*(2 Pi/m)]}, {nos, 0, m - 1,
1}];
r = Table[
Text[Style[ToString@nos, Medium], q[[nos + 1]] 1.1], {nos, 0,
m - 1, 1}];
Graphics[{{Blue, Circle[{0, 0}, 1.3]}, {AbsoluteThickness[2],
Line[p]},(*{Thickness[.0015],Line[
s]},*), {Red, AbsolutePointSize[5], Point[q]}, {Brown, r}},
ImageSize -> {500, 500}], {{m, 10}, 10, 20, 2,
Appearance -> "Labeled"}, {{tstart, 2}, 2, 10, 1,
Appearance -> "Labeled"}]
where the Print[u] and Print [p] lines are only there to try and see what the problem is with the code. (right now when the code is executed a circle with m points shows up on the screen, but no lines appear and there is the message from mathematica
{{-Cos[1/5 π Mod[List,10.]],Sin[1/5 π Mod[List,10.]]},{-0.809017,0.587785}}
is not a point that can be plotted.)
Any help would be awesome since this is my first project ever using Mathematica. Thank you so much!
1,m
not0,m-1
mathematica indexes lists starting with 1. You're gettingList
becauseu[[0]]
is the head of the expression, which in this case isList
. $\endgroup$