0
$\begingroup$

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!

$\endgroup$
1
  • $\begingroup$ Your tables should go from 1,m not 0,m-1 mathematica indexes lists starting with 1. You're getting List because u[[0]] is the head of the expression, which in this case is List. $\endgroup$
    – N.J.Evans
    Commented Aug 25, 2017 at 17:52

1 Answer 1

2
$\begingroup$

Here's a more idiomatic way to get your result:

Define a collatz function that takes an integer n checks it for even, or oddness and applies the appropriate transformation:

collatz[n_Integer]/;EvenQ@n:=n/2;
collatz[n_Integer]/;OddQ@n:=3*n+1;

Then use NestWhileList to calculate the sequence u conditionally. (This is one idiomatic replacement for loops that you should get familiar with.)

Then use map(i.e. /@ operator) to apply your transformation to geometric coordinates using the fact that Mod is Listable. Map is another idiomatic loop replacement that lets you dispense with indices.

Manipulate[
 u = NestWhileList[collatz, tstart, # > 1 &];
 ln = {Cos[-#], Sin[#]} & /@(Mod[u, m]*2 π/m);
 Graphics[{
   Line@ln,
   Blue, Circle[{0, 0}, 1.3]
   }
  , PlotRange -> All
  ]
 , {{tstart, 2}, 2, 10, 1, Appearance -> "Labeled"}
 , {{m, 10}, 2, 20, 2, Appearance -> "Labeled"}
 ]

enter image description here

You can add the other embellishments to the image.

I'd suggest reading this post what-are-the-most-common-pitfalls-awaiting-new-users to get an idea of where to start with more idiomatic MMA constructions. And of course, welcome to MMA!

$\endgroup$
1
  • $\begingroup$ Thank you so much!! This worked perfectly, thank you for the idea of making a specific function for the Collatz conjecture. This really helped. $\endgroup$
    – JonHales
    Commented Aug 25, 2017 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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