Search code examples
arrayspari-gp

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


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?


Solution

  • 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.)