<div dir="ltr">Hello Samuel,<div><br></div><div>The size of ns (number of steps) and seq (sequence of values) are variable depending on the integer input, and this seems to be one issue. So I guess the problem is how to allocate dynamic arrays where you do not know the final size?</div><div><br></div><div>This is a direct translation of a Matlab function:</div><div><span style="font-family:Monospaced;color:rgb(176,24,19)"><br></span></div><div><span style="font-family:Monospaced;color:rgb(176,24,19)">function</span><span style="font-family:Monospaced"> </span><span style="font-family:Monospaced;color:rgb(74,85,219)">[</span><span style="font-family:Monospaced;color:rgb(131,67,16);font-weight:bold">ns</span><span style="font-family:Monospaced;color:rgb(0,0,0)">, </span><span style="font-family:Monospaced;color:rgb(131,67,16);font-weight:bold">seq</span><span style="font-family:Monospaced;color:rgb(74,85,219)">]</span><span style="font-family:Monospaced;color:rgb(92,92,92)">=</span><span style="font-family:Monospaced;color:rgb(0,0,0);text-decoration-line:underline">collatz</span><span style="font-family:Monospaced;color:rgb(74,85,219)">(</span><span style="font-family:Monospaced;color:rgb(131,67,16);font-weight:bold">n</span><span style="font-family:Monospaced;color:rgb(74,85,219)">)</span><br></div><div><pre style="font-family:Monospaced"><span style="color:rgb(100,174,100);font-style:italic">// Translation of Matlab function</span>
<span style="color:rgb(100,174,100);font-style:italic">// First number in the sequence is n</span>
<span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(131,67,16);font-weight:bold">n</span><span style="color:rgb(0,0,0)">;</span>
<span style="color:rgb(100,174,100);font-style:italic">// Position an index on the next element of the sequence</span>
<span style="color:rgb(0,0,0)">i</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(188,143,143)">2</span><span style="color:rgb(0,0,0)">;</span> 

<span style="color:rgb(100,174,100);font-style:italic">// Repeat the iteration until you find a 1</span>
<span style="color:rgb(160,32,240)">while</span> <span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(92,92,92)">-</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">~=</span> <span style="color:rgb(188,143,143)">1</span>
    <span style="color:rgb(100,174,100);font-style:italic">// Use a modulus after division to find an even/odd number</span>
    <span style="color:rgb(160,32,240)">if</span> <span style="color:rgb(174,92,176);text-decoration-line:underline">modulo</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(92,92,92)">-</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(0,0,0)">,</span> <span style="color:rgb(188,143,143)">2</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">==</span> <span style="color:rgb(188,143,143)">0</span>
        <span style="color:rgb(100,174,100);font-style:italic">// Step taken if even</span>
        <span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(92,92,92)">-</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(92,92,92)">/</span><span style="color:rgb(188,143,143)">2</span><span style="color:rgb(0,0,0)">;</span>
    <span style="color:rgb(160,32,240)">else</span>
        <span style="color:rgb(100,174,100);font-style:italic">// Step taken if odd</span>
        <span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(188,143,143)">3</span><span style="color:rgb(92,92,92)">*</span><span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(92,92,92)">-</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">+</span> <span style="color:rgb(188,143,143)">1</span><span style="color:rgb(0,0,0)">;</span>
    <span style="color:rgb(160,32,240)">end</span>
    <span style="color:rgb(100,174,100);font-style:italic">// Increment index</span>
    <span style="color:rgb(0,0,0)">i</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(0,0,0)">i</span><span style="color:rgb(92,92,92)">+</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(0,0,0)">;</span>
<span style="color:rgb(160,32,240)">end</span>
<span style="color:rgb(100,174,100);font-style:italic">// Find the length of the sequence</span>
<span style="color:rgb(131,67,16);font-weight:bold">ns</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(50,185,185)">length</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(131,67,16);font-weight:bold">seq</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(0,0,0)">;</span>
<span style="color:rgb(176,24,19)">endfunction</span></pre></div><div><br></div><div>Lester</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 15 Aug 2021 at 09:24, Samuel Gougeon <<a href="mailto:sgougeon@free.fr">sgougeon@free.fr</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
  
    
  
  <div>
    <div>Le 15/08/2021 à 09:00, Lester Anderson
      a écrit :<br>
    </div>
    <blockquote type="cite">
      
      <div dir="ltr">Hello,
        <div><br>
        </div>
        <div>Basic query. I have a simple code that applies the Collatz
          conjecture equation (3n+1) by running a function and then runs
          a loop over the values stored in prime (the first 8 Prime
          numbers):</div>
        <div><br>
        </div>
        <div><span style="color:rgb(50,185,185);font-family:Monospaced">clear</span><br>
        </div>
        <div>
          <pre style="font-family:Monospaced"><span style="color:rgb(50,185,185)">exec</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(188,143,143)">'</span><span style="color:rgb(188,143,143)">collatz.sci</span><span style="color:rgb(188,143,143)">'</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(92,92,92)">-</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(0,0,0)">;</span>

<span style="color:rgb(0,0,0)">prime</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(174,92,176);text-decoration-line:underline">primes</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(188,143,143)">20</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(0,0,0)">; </span>

<span style="color:rgb(160,32,240)">for</span> <span style="color:rgb(0,0,0)">i</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(188,143,143)">1</span><span style="color:rgb(255,170,0)">:</span><span style="color:rgb(50,185,185)">length</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">prime</span><span style="color:rgb(74,85,219)">)</span>
    <span style="color:rgb(74,85,219)">[</span><span style="color:rgb(0,0,0)">ns</span><span style="color:rgb(0,0,0)">,</span> <span style="color:rgb(0,0,0)">seq</span><span style="color:rgb(74,85,219)">]</span><span style="color:rgb(92,92,92)">=</span><span style="color:rgb(0,0,0)">collatz</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">prime</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(74,85,219)">)</span>
<span style="color:rgb(160,32,240)">end</span></pre>
          As it stands, this just runs to the end (i=8) and the value
          19. How can I get the code to write the results of each loop
          pass into the variables ns and seq, such that each contains
          the results of the 8 passes?</div>
      </div>
    </blockquote>
    <p><br>
      This runs all the 8 iterations, but each next iteration overwrites
      ns and seq.<br>
      What are the sizes of ns and seq ?<br>
    </p>
    <br>
  </div>

_______________________________________________<br>
users mailing list<br>
<a href="mailto:users@lists.scilab.org" target="_blank">users@lists.scilab.org</a><br>
<a href="http://lists.scilab.org/mailman/listinfo/users" rel="noreferrer" target="_blank">http://lists.scilab.org/mailman/listinfo/users</a><br>
</blockquote></div>