[Scilab-users] Loop query
Stefan Du Rietz
sdr at durietz.se
Sun Aug 15 16:32:54 CEST 2021
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
More information about the users
mailing list