Hi Samuel, Further to your method, is there a way to vectorise the list output based on the indices?
One can access the elements as seq(1), seq(5) etc. I did look at list2vec(seq), but as documented this creates a single column vector. Thanks Lester On Sun, 15 Aug 2021 at 14:55, Samuel Gougeon <[email protected]> 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 > endendfunction > 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 > > > _______________________________________________ > users mailing list > [email protected] > http://lists.scilab.org/mailman/listinfo/users >
_______________________________________________ users mailing list [email protected] http://lists.scilab.org/mailman/listinfo/users
