2

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?

geeceekay
  • 67
  • 8

2 Answers2

5

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

Charles
  • 11,269
  • 13
  • 67
  • 105
-2
x = [];
for (i=1,10, print("test " i ": " x[i]))
  • Hi it doesn't work when accessing `x[1]` (apparently `x=[];` doesn't allocate any memory for the array) – reuns Oct 01 '17 at 21:21