how to use more than one sequence in forstep statement in PARI/GP?

233 views Asked by At

I am trying to write something in PARI/GP. I want to create a vector with the values from 1000 to 41000 in steps of 3000.

Therefore, I wanted to use the forstep statement. This works fine if you use 1 sequence.

Example:

forstep(x=1000,41000,3000,print(x))

However, I want to do something like:

forstep(x=1000,41000,3000,x[i]=x & i=i+1)

How to do this?

1

There are 1 answers

1
Jeppe Stig Nielsen On

One way of making your approach work, is:

v=vector(14); i=1; forstep(x=1000,41000,3000,(v[i]=x) & (i=i+1)); v

I put a parenthesis around each assignment. Otherwise, PARI/GP sees it as v[i]=(x & (i=i+1)). So in some cases you can combine two expressions with &.

However: The operator & will short-circuit if its first operand is zero (seen as false). So the answer you are looking for, is the semicolon ;. So:

v=vector(14); i=1; forstep(x=1000,41000,3000,v[i]=x; i=i+1); v

I believe a construct with semicolon like v[i]=x; i=i+1 is called a sequence in the PARI/GP terminology.