Re: [Scilab-users] Loop query

2021-08-15 Thread Lester Anderson
Thanks for all the guidance guys!

Lester

On Sun, 15 Aug 2021 at 15:33, Stefan Du Rietz  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@lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Stefan Du Rietz



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@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Stefan Du Rietz

One solution:

// Run the last (largest):
[ns, seq] = collatz(prime($))

// insert in new zeros vector:
ns = [zeros(prime-1), ns]
// insert seq in new zeros matrix:
seq = [zeros(length(seq), length(ns)-1), seq];

for  i = 1:length(prime)-1
  // Run collatz to get the new ns:
  [nsi, seqi] = collatz(prime(i));
  // insert in vector ns and matrix seq:
  ns(i) = nsi;
  seq(1:nsi, i) = seqi;
end

dispalay ns above seq:
[ns; seq]

Stefan


On 2021-08-15 13:03, Lester Anderson wrote:

Hi Stefan,

Thank you for clarifying the meaning of the error message.

Lester

On Sun, 15 Aug 2021 at 11:59, Stefan Du Rietz > wrote:


Hello Lester,
the problem is that seq in each loop is a vector of increasing length!

Stefan


On 2021-08-15 12:05, Lester Anderson wrote:
 > Hi Stefan,
 >
 > I did try that before, but got an error - "Submatrix incorrectly
defined"
 >
 > Lester
 >
 > On Sun, 15 Aug 2021 at 10:56, Stefan Du Rietz mailto:s...@durietz.se>
 > >> wrote:
 >
 >
 >
 >     On 2021-08-15 09:00, Lester Anderson wrote:
 >      > Hello,
 >      >
 >      > 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):
 >      >
 >      > clear
 >      >
 >      > exec('collatz.sci',-1);
 >      >
 >      > prime  =  primes(20);
 >      >
 >      > for  i  =  1:length(prime)
 >      >      [ns,  seq]=collatz(prime(i))
 >      > end
 >      >
 >      > 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?
 >      >
 >      > Can no longer search the forums online from the website.
 >      >
 >      > Thanks
 >      > Lester
 >
 >
 >     // Before the for loop:
 >     ns = zeros(prime);
 >     seq = ns;
 >
 >     // Changed for loop
 >     for  i  =  1:length(prime)
 >           [ns(i),  seq(i)]=collatz(prime(i))
 >     end
 >
 >     Regards
 >     Stefan
 >     ___
 >     users mailing list
 > users@lists.scilab.org 
>
 > http://lists.scilab.org/mailman/listinfo/users

 >     >
 >
 >
 > ___
 > users mailing list
 > users@lists.scilab.org 
 > http://lists.scilab.org/mailman/listinfo/users

 >
___
users mailing list
users@lists.scilab.org 
http://lists.scilab.org/mailman/listinfo/users



___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Samuel Gougeon

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


___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Lester Anderson
Hi Stefan,

Thank you for clarifying the meaning of the error message.

Lester

On Sun, 15 Aug 2021 at 11:59, Stefan Du Rietz  wrote:

> Hello Lester,
> the problem is that seq in each loop is a vector of increasing length!
>
> Stefan
>
>
> On 2021-08-15 12:05, Lester Anderson wrote:
> > Hi Stefan,
> >
> > I did try that before, but got an error - "Submatrix incorrectly defined"
> >
> > Lester
> >
> > On Sun, 15 Aug 2021 at 10:56, Stefan Du Rietz  > > wrote:
> >
> >
> >
> > On 2021-08-15 09:00, Lester Anderson wrote:
> >  > Hello,
> >  >
> >  > 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):
> >  >
> >  > clear
> >  >
> >  > exec('collatz.sci',-1);
> >  >
> >  > prime  =  primes(20);
> >  >
> >  > for  i  =  1:length(prime)
> >  >  [ns,  seq]=collatz(prime(i))
> >  > end
> >  >
> >  > 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?
> >  >
> >  > Can no longer search the forums online from the website.
> >  >
> >  > Thanks
> >  > Lester
> >
> >
> > // Before the for loop:
> > ns = zeros(prime);
> > seq = ns;
> >
> > // Changed for loop
> > for  i  =  1:length(prime)
> >   [ns(i),  seq(i)]=collatz(prime(i))
> > end
> >
> > Regards
> > Stefan
> > ___
> > users mailing list
> > users@lists.scilab.org 
> > http://lists.scilab.org/mailman/listinfo/users
> > 
> >
> >
> > ___
> > users mailing list
> > users@lists.scilab.org
> > http://lists.scilab.org/mailman/listinfo/users
> >
> ___
> users mailing list
> users@lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Stefan Du Rietz

Hello Lester,
the problem is that seq in each loop is a vector of increasing length!

Stefan


On 2021-08-15 12:05, Lester Anderson wrote:

Hi Stefan,

I did try that before, but got an error - "Submatrix incorrectly defined"

Lester

On Sun, 15 Aug 2021 at 10:56, Stefan Du Rietz > wrote:




On 2021-08-15 09:00, Lester Anderson wrote:
 > Hello,
 >
 > 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):
 >
 > clear
 >
 > exec('collatz.sci',-1);
 >
 > prime  =  primes(20);
 >
 > for  i  =  1:length(prime)
 >      [ns,  seq]=collatz(prime(i))
 > end
 >
 > 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?
 >
 > Can no longer search the forums online from the website.
 >
 > Thanks
 > Lester


// Before the for loop:
ns = zeros(prime);
seq = ns;

// Changed for loop
for  i  =  1:length(prime)
      [ns(i),  seq(i)]=collatz(prime(i))
end

Regards
Stefan
___
users mailing list
users@lists.scilab.org 
http://lists.scilab.org/mailman/listinfo/users



___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Lester Anderson
Hi Stefan,

I did try that before, but got an error - "Submatrix incorrectly defined"

Lester

On Sun, 15 Aug 2021 at 10:56, Stefan Du Rietz  wrote:

>
>
> On 2021-08-15 09:00, Lester Anderson wrote:
> > Hello,
> >
> > 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):
> >
> > clear
> >
> > exec('collatz.sci',-1);
> >
> > prime  =  primes(20);
> >
> > for  i  =  1:length(prime)
> >  [ns,  seq]=collatz(prime(i))
> > end
> >
> > 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?
> >
> > Can no longer search the forums online from the website.
> >
> > Thanks
> > Lester
>
>
> // Before the for loop:
> ns = zeros(prime);
> seq = ns;
>
> // Changed for loop
> for  i  =  1:length(prime)
>  [ns(i),  seq(i)]=collatz(prime(i))
> end
>
> Regards
> Stefan
> ___
> users mailing list
> users@lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Stefan Du Rietz



On 2021-08-15 09:00, Lester Anderson wrote:

Hello,

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):


clear

exec('collatz.sci',-1);

prime  =  primes(20);

for  i  =  1:length(prime)
 [ns,  seq]=collatz(prime(i))
end

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?


Can no longer search the forums online from the website.

Thanks
Lester



// Before the for loop:
ns = zeros(prime);
seq = ns;

// Changed for loop
for  i  =  1:length(prime)
[ns(i),  seq(i)]=collatz(prime(i))
end

Regards
Stefan
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Lester Anderson
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. So I guess
the problem is how to allocate dynamic arrays where you do not know the
final size?

This is a direct translation of a Matlab function:

function [ns, seq]=collatz(n)

// Translation of Matlab function// First number in the sequence is
nseq(1) = n;// Position an index on the next element of the sequencei
= 2;
// Repeat the iteration until you find a 1while seq(i-1) ~= 1
// Use a modulus after division to find an even/odd number
if modulo(seq(i-1), 2) == 0
// Step taken if even
seq(i) = seq(i-1)/2;
else
// Step taken if odd
seq(i) = 3*seq(i-1) + 1;
end
// Increment index
i = i+1;end// Find the length of the sequencens = length(seq);endfunction


Lester

On Sun, 15 Aug 2021 at 09:24, Samuel Gougeon  wrote:

> Le 15/08/2021 à 09:00, Lester Anderson a écrit :
>
> Hello,
>
> 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):
>
> clear
>
> exec('collatz.sci',-1);
> prime = primes(20);
> for i = 1:length(prime)
> [ns, seq]=collatz(prime(i))end
>
> 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?
>
>
> This runs all the 8 iterations, but each next iteration overwrites ns and
> seq.
> What are the sizes of ns and seq ?
>
> ___
> users mailing list
> users@lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


Re: [Scilab-users] Loop query

2021-08-15 Thread Samuel Gougeon

Le 15/08/2021 à 09:00, Lester Anderson a écrit :

Hello,

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):


clear
exec('collatz.sci',-1);

prime  =  primes(20);

for  i  =  1:length(prime)
 [ns,  seq]=collatz(prime(i))
end
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?



This runs all the 8 iterations, but each next iteration overwrites ns 
and seq.

What are the sizes of ns and seq ?


___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


[Scilab-users] Loop query

2021-08-15 Thread Lester Anderson
Hello,

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):

clear

exec('collatz.sci',-1);
prime = primes(20);
for i = 1:length(prime)
[ns, seq]=collatz(prime(i))end

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?

Can no longer search the forums online from the website.

Thanks
Lester
___
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users