[Scilab-users] Loop query
Lester Anderson
arctica1963 at gmail.com
Sun Aug 15 18:15:15 CEST 2021
Thanks for all the guidance guys!
Lester
On Sun, 15 Aug 2021 at 15:33, Stefan Du Rietz <sdr at durietz.se> wrote:
>
>
> On 2021-08-15 15:54, Samuel Gougeon wrote:
> > Le 15/08/2021 à 11:28, Lester Anderson a écrit :
> >> Hello Samuel,
> >>
> >> 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.
> >
> > For this reason, seq must be a list, leading to
> >
> > function [ns, seq] = collatz(p)
> > seq = p
> > while %T
> > if pmodulo(p, 2)
> > p = p*3+1
> > else
> > p = p/2
> > end
> > seq = [seq p]
> > if p==1
> > ns = length(seq)
> > break
> > end
> > end
> > endfunction
> >
> > prime = primes(20); [ns, seq] = ([],list()); for i = 1:length(prime)
> > [ns(i), seq(i)] = collatz(prime(i));
> > end --> ns' ans = 2. 8. 6. 17. 15. 10. 13. 21. --> seq seq = (1) = [2,1]
> > (2) = [3,10,5,16,8,4,2,1] (3) = [5,16,8,4,2,1] (4) =
> > [7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1] (5) =
> > [11,34,17,52,26,13,40,20,10,5,16,8,4,2,1] (6) =
> > [13,40,20,10,5,16,8,4,2,1] (7) = [17,52,26,13,40,20,10,5,16,8,4,2,1] (8)
> > = [19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
> >
> > Samuel
> >
> Of course, Samuel, I didn't realize that the length of seq was not
> monotonically increasing (which I had if I had looked at the displayed
> matrix ...)!
>
> Stefan
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20210815/6064eb2d/attachment.htm>
More information about the users
mailing list