Search code examples
pari-gppari

PARI/GP: How to get the max prime factor of the integer?


I am new to pari/gp. I use factorint to find all the prime factors and it returns a matrix. I am trying to traverse through a matrix to find the largest number inside but unable to find the length of rows and columns. Also how can i use the if to compare each element is higher or lower. My p is being generated on top.

temp = factorint(p-1);
num = 0;
for(i=1, size,
    for(j=1, size,
        if(num <= temp[i,j], num = temp[i,j]);
    );
);

print("number is = "  num);

Thanks in advance.


Solution

  • Please, note that factorint(p) always returns the nx2-matrix, where n is the number of the prime factors for p. First column is for the prime factors. Second column is for their multiplicities.

    So all you need is to find the maximum element of the first column. It can be done as follows:

    factors = factorint(p-1);
    print("number is = ", vecmax(factors[, 1]));
    

    By the way, the length of vector v is just #v in PARI/GP.