<div dir="ltr">Hello all,<br><br>I have prepared a code to generate Collatz sequences based on the even (n/2) and odd ((3n+1)/2) rules.<br><br>clear<br><br>function [ns, seq] = collatz(n)<br>// Reference: <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">https://en.wikipedia.org/wiki/Collatz_conjecture</a><br>//ns =number of steps; seq=Collatz sequence<br>seq(1) = n;<br>// Position an index on the next element of the sequence<br>i = 2; <br><br>// Repeat the iteration until you find a 1<br>while seq(i-1) ~= 1<br>    // Use modulo to define even/odd numbers<br>    if modulo(seq(i-1), 2) == 0<br>        // Step taken if even<br>        seq(i) = seq(i-1)/2;<br>    else<br>        // Step taken if odd<br>        // seq(i) = 3*seq(i-1) + 1 // "3n+1 Collatz"<br>        seq(i) = (3*seq(i-1) + 1)/2; // "shortcut form Collatz"<br>    end<br>    // Increment index<br>    i = i+1;<br>end<br>// Find the length of the sequence<br>ns = length(seq);<br>endfunction<br><br>n = input('Enter a positive integer: ')<br>tic()<br><br>for  i = 1:n<br>   [nsi, seqi] = collatz(i);<br>   ns(i) = nsi;<br>   seq(1:nsi, i) = seqi;<br>end<br><br>// Find maxima in each Collatz sequence <br>peak_values = max(seq, 'r')<br><br>t=toc()<br>mprintf('Elapsed time: %4.8f\n',t)<br><br>What I would like to try is apply a further rule to generate a plot<br>based on the Collatz sequences using left/right angular turn depending on<br>whether the number is odd or even, going in reverse (starting at 1) and branching out.<br><br>I can easily define the odd/even numbers in the sequence (modulo), but not sure how to<br>apply the angle rotation for the graphic plot. Any pointers would be helpful.<div><br></div><div>Attached an example image by way of reference.</div><div><br></div><div>On a secondary point, the Collatz conjecture can be applied to negative values, but would be interested to know how the code has to be tweaked. If you stick a negative value in the code as is, it gets stuck in a loop.<br><br>Thanks<br>Lester<br><br></div></div>