Check if a number is a perfect power of another number
<p>For example, 243 is a perfect power of 3 because 243=3^5.</p>

<p>I've previously been using <code>(math.log(a) / math.log(b)).is_integer()</code>, which I thought worked fine, but then I tried it with the example above and it actually returns 4.999999999999999 due to floating point arithmetic. So it's only reliable for very small numbers, less than around 100 I've found.</p>

<p>I suppose I could use a loop to do repeated multiplication... i.e. set i to 3, then 9, then 27, then 81, then 243, which equals the target, so we know it's a perfect power. If it reaches a point where it's bigger than 243 then we know it's not a perfect power. But I'm running this check within a loop as it is, so this seems like it'd be very inefficient.</p>

<p>So is there any other way of reliably checking if a number is a perfect power of another?</p>

<p>Try:</p>

<pre><code>b ** int(round(math.log(a, b))) == a
</code></pre>

<p>That is, only use <code>log()</code> (note there is a 2-argument form!) to get a guess at an integer power, then verify whether "that works".</p>

<p>Note that <code>math.log()</code> returns a sensible result even for integer arguments much too large to represent as a float.  Also note that integer <code>**</code> in Python is exact, and uses an efficient algorithm internally (doing a number of multiplications proportional to the number of bits in the exponent).</p>

<p>This is straightforward and much more efficient (in general) than, say, repeated division.</p>

<p>But then I'm answering the question you asked ;-)  If you had some other question in mind, some of the other answers may be more appropriate.</p>

python math