[Scilab-users] Loop query
Samuel Gougeon
sgougeon at free.fr
Sun Aug 15 15:54:58 CEST 2021
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20210815/ff72a7b9/attachment.htm>
More information about the users
mailing list