I'm trying to create a diagram that could be used to visualize the Collatz Conjecture in a new way, but I can't get Manipulate to work in the way I want it to. The goal of the program is to have m points in a circle, and be able to choose a starting point t, and go around the circle as defined by the Collatz conjecture, (i.e. if t is even to move to the point t/2 and if t is odd move to the point 3*t+1) but to move around the circle mod m.

Here is the code that I have right now.

g = Manipulate[

u = {};
n = 0;
While [t != 1, 

  If[Mod[t, 2] == 0 && t < 1000, t = (t/2); n = n + 1; 
  AppendTo[u, n]; Print[u] , t = 3 t + 1; n = n + 1; 
  AppendTo[u, n]; Print[u], Break]]
  p = Table[{{-Cos[nos *(2 Pi/m)], 
  Sin[nos *(2 Pi/m)]}, {-Cos[N[Mod[Part[u, nos], m]] *(2 Pi/m)], 
  Sin[N[Mod[Part[u, nos], m]] *(2 Pi/m)]}}, {nos, 1, Length[u], 
 q = Table[{-Cos[nos *(2 Pi/m)], Sin[nos *(2 Pi/m)]}, {nos, 0, m - 1,
 r = Table[
Text[Style[ToString@ nos, Medium], q[[nos + 1]] 1.1], {nos, 0, 
 m - 1, 1}];
 Graphics[{{White, Circle[{0, 0}, 1.3]}, {Thickness[.0015], 
     Line[p]}, {Thickness[.0015], Line[s]}, {Hue[0], PointSize[.005], 
     Point[q]}, {Brown, r}
     }, ImageSize -> {600, 600}], {m, 10, 500, 1}, {t, 2, 100, 1}]

as of right now though, the program just says "running" but never produces the image with the option of manipulating.

Any tips or advice would be really helpful! I'm brand new to using Mathematica.


1 Answer 1


There are a number of bugs in your code. So many that I'm not sure how to fix it. I made some guesses about what you were trying to do and came up with the following. Even it isn't really want you want, maybe it can still serve as base from which you move forward.

  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]]];
  p =
    With[{nmax = Mod[Last[u], m]},
      Table[{-Cos[nos*(2 Pi/m)], Sin[nos*(2 Pi/m)]}, {nos, 0, nmax}]];
  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}];
    {{Blue, Circle[{0, 0}, 1.3]},
     {AbsoluteThickness[2], Line[p]},
     {Red, AbsolutePointSize[5], Point[q]},
     {Brown, r}},
    ImageSize -> {500, 500}],
  {{m, 10}, 10, 20, 2, Appearance -> "Labeled"},
  {{tstart, 2}, 2, 10, 1, Appearance -> "Labeled"}]



  • Your major problems were a malformed While-loop, a missing semicolon, and trying use the variable t as a control variable and a computation variable.
  • I have made many of your numeric parameters much smaller. It is good to use small parameters when debugging. Hold off from large numbers until you are pretty sure you have things working.
  • I commented out displaying line s. You give no definition for it and I have no clue what it is.
  • A white circle will not show in the graphics view. I have used blue.
  • I made a guess about what poly-line p should be.
  • Assigning the Manipulate expression to a variable is meaningless; it returns Null.
  • I don't like all the global variables you used, but I didn't do anything about it. They work but I prefer to keep all variables in a Manipulate expression localized. Unfortunately, I don't have the time to add a lesson on localization to this answer.
  • You really didn't do such a bad job for a beginner. Using Manipulate to demonstrate the Collatz conjecture in an interesting way is an ambitious project for a beginner. I hope completing it goes well.
  • $\begingroup$ Thank you so much for your quick reply and for your great explanation! like I said I've never used Mathematica before, just a little bit of Python and C++, but your answer was very clear and useful. Thank you so much! $\endgroup$
    – JonHales
    Aug 25, 2017 at 16:10

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.