ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 31 Jan 2012 20:52:12 +0100exponent of a primehttps://ask.sagemath.org/question/8685/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.Tue, 31 Jan 2012 17:00:19 +0100https://ask.sagemath.org/question/8685/exponent-of-a-prime/Answer by achrzesz for <p>I am newbie in SAGE and here. My apologies for any mistake. This is an easy question. </p>
<p>Is there a command that for an integer $m$ and a prime number $p$ returns the exponent of $p$ in $m$?</p>
<p>If not, I would like if the following definition can be improved.</p>
<pre><code>def exponent(p,m):
v=m
sol=0
while v%p==0:
v=v/p
sol=sol+1
return sol
</code></pre>
<p>Thanks.</p>
https://ask.sagemath.org/question/8685/exponent-of-a-prime/?answer=13221#post-id-13221sage: 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)]Tue, 31 Jan 2012 17:38:08 +0100https://ask.sagemath.org/question/8685/exponent-of-a-prime/?answer=13221#post-id-13221Comment by kcrisman for <p>sage: m=240</p>
<p>sage: m.valuation(2) <br/>
4</p>
<p>sage: m.valuation(3)</p>
<p>1</p>
<p>sage: m.valuation(5)</p>
<p>1</p>
<p>sage: [x for x in factor(m)] </p>
<p>[(2, 4), (3, 1), (5, 1)]</p>
https://ask.sagemath.org/question/8685/exponent-of-a-prime/?comment=20373#post-id-20373Maybe 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.Tue, 31 Jan 2012 20:36:21 +0100https://ask.sagemath.org/question/8685/exponent-of-a-prime/?comment=20373#post-id-20373Comment by DSM for <p>sage: m=240</p>
<p>sage: m.valuation(2) <br/>
4</p>
<p>sage: m.valuation(3)</p>
<p>1</p>
<p>sage: m.valuation(5)</p>
<p>1</p>
<p>sage: [x for x in factor(m)] </p>
<p>[(2, 4), (3, 1), (5, 1)]</p>
https://ask.sagemath.org/question/8685/exponent-of-a-prime/?comment=20374#post-id-20374This 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.Tue, 31 Jan 2012 17:47:04 +0100https://ask.sagemath.org/question/8685/exponent-of-a-prime/?comment=20374#post-id-20374Comment by DSM for <p>sage: m=240</p>
<p>sage: m.valuation(2) <br/>
4</p>
<p>sage: m.valuation(3)</p>
<p>1</p>
<p>sage: m.valuation(5)</p>
<p>1</p>
<p>sage: [x for x in factor(m)] </p>
<p>[(2, 4), (3, 1), (5, 1)]</p>
https://ask.sagemath.org/question/8685/exponent-of-a-prime/?comment=20372#post-id-20372Do 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. Tue, 31 Jan 2012 20:52:12 +0100https://ask.sagemath.org/question/8685/exponent-of-a-prime/?comment=20372#post-id-20372