0

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.

Piotr Semenov
  • 1,761
  • 16
  • 24
Tommy Yap
  • 85
  • 1
  • 9

2 Answers2

1

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.

Piotr Semenov
  • 1,761
  • 16
  • 24
  • 1
    Can it occur that the max prime is not the one in the last row? Then `factors[matsize(factors)[1],1]` can be used. – Jeppe Stig Nielsen May 29 '16 at 20:13
  • Assuming that p - 1 > 1, then `factors` is a non-empty matrix, its first column only contains primes and they are sorted in increasing order. Under these assumptions, your comment and the answer are correct. – K.B. Nov 04 '21 at 01:33
0

Besides matsize, you could also use #. For example, factorint(30) gives a 3x2 matrix;

[2 1]

[3 1]

[5 1]

#factorint(30) gives the number of colums of that matrix (which is 2). By transposing the matrix, the resulting matrix has 3 columns, which is the number of distinct prime factors of 30. Transposing can be done as follows: append ~ to a matrix. So we could do #factorint(30)~ to get the number of distinct prime factors; which prints

[2 3 5]

[1 1 1]

As those prime factors will be increasingly ordered in the first row, the last one in the first row is the largest one hence factorint(30)[#factorint(30)~, 1] gives the largest prime factor of 30; 5 Now you could avoid factoring 30 twice by doing;

f = factorint(30); f[#f~]

to get 5 as desired.