Ask Your Question

exponent of a prime

asked 2012-01-31 10:00:19 -0500

emiliocba gravatar image

updated 2012-01-31 10:07:45 -0500

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):
    while v%p==0:
    return sol


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-01-31 10:38:08 -0500

achrzesz gravatar image

sage: m=240

sage: m.valuation(2)

sage: m.valuation(3)


sage: m.valuation(5)


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

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

edit flag offensive delete link 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.

DSM gravatar imageDSM ( 2012-01-31 10:47:04 -0500 )edit

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.

kcrisman gravatar imagekcrisman ( 2012-01-31 13:36:21 -0500 )edit

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.

DSM gravatar imageDSM ( 2012-01-31 13:52:12 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-01-31 10:00:19 -0500

Seen: 197 times

Last updated: Jan 31 '12