How do I declare and call array in PARI/GP?

1.5k views Asked by At

How do I declare and call array in PARI/GP?

For example, I have the following in java:

int[] myArray = new int[5];
for(int i = 0; i < 5; i++){
   myArray[i] = i + 5;
}

How do I do the same thing while using PARI/GP?

2

There are 2 answers

0
Charles On BEST ANSWER

The usual way is

myArray = vector(5, i, i+4);

where I have replaced i+5 with i+4 because GP vectors are 1-based, not 0-based.

You could also do

myArray = vector(5);
for(i=1,5, myArray[i] = i+4);

if you prefer. (This is useful in some cases, e.g., when you want to refer to earlier values in the array.)

1
user3001342 On
x = [];
for (i=1,10, print("test " i ": " x[i]))