# exponent of a prime I am newbie in SAGE and here. My apologies for any mistake. This is an easy question.

Is there a command that for an integer $m$ and a prime number $p$ returns the exponent of $p$ in $m$?

If not, I would like if the following definition can be improved.

def exponent(p,m):
v=m
sol=0
while v%p==0:
v=v/p
sol=sol+1
return sol


Thanks.

edit retag close merge delete

Sort by » oldest newest most voted

sage: m=240

sage: m.valuation(2)
4

sage: m.valuation(3)

1

sage: m.valuation(5)

1

sage: [x for x in factor(m)]

[(2, 4), (3, 1), (5, 1)]

more

This works but is absurdly obscure. You shouldn't need to know to search for "p-adic valuation" to get the right answer. We should come up with a good name and make an alias as an Integer method.

Maybe an alias? But I have to say that the way I would do it is the last one. I suppose you could open a ticket to do this.

Do you mean the factor approach? Sure, but then if you have (2^10)*p*q for big p,q you'l spend all your time factoring. I've actually needed this in the past for OEIS stuff and wound up taking log(n/n.prime_part_to_m(p),p) or whatever the prime_part method is called.