Search code examples
pari-gp

Highest power of prime dividing a number with PARI/GP


How do I find the highest power of a given prime p dividing a number N with PARI/GP?

E. g. if we have p = 7 and N = 3087 we get e = 3 with p^e | N but p^(e+1) not dividing N.

I want to avoid a full factorization of the number N.


Solution

  • Use the valuation command, like so:

    valuation(3087, 7)
    

    This does not compute the factorization of the number.

    Alternately, you could write your own function:

    val(n, p)=
    {
      if(n==0, return(+oo));
      my(e);
      while(n%p==0,
        n /= p;
        e++
      );
      e;
    }