JohnnyQ - 4 months ago 18

Python Question

I am trying to complete the following exercise:

https://www.codewars.com/kata/whats-a-perfect-power-anyway/train/python

I tried multiple variations, but my code breaks down when big numbers are involved (I tried multiple variations with solutions involving log and power functions):

**Exercise:**

Your task is to check wheter a given integer is a perfect power. If it is a perfect power, return a pair m and k with m^k = n as a proof. Otherwise return Nothing, Nil, null, None or your language's equivalent.

Note: For a perfect power, there might be several pairs. For example 81 = 3^4 = 9^2, so (3,4) and (9,2) are valid solutions. However, the tests take care of this, so if a number is a perfect power, return any pair that proves it.

The exercise uses Python 3.4.3

**My code:**

`import math`

def isPP(n):

for i in range(2 +n%2,n,2):

a = math.log(n,i)

if int(a) == round(a, 1):

if pow(i, int(a)) == n:

return [i, int(a)]

return None

How is it possible that I keep getting incorrect answers for bigger numbers? I read that in Python 3, all ints are treated as "long" from Python 2, i.e. they can be very large and still represented accurately. Thus, since i and int(a) are both ints, shouldn't the pow(i, int(a)) == n be assessed correctly? I'm actually baffled.

Answer

you are in the right track with logarithm but you are doing the math wrong, also you are skipping number you should not and only testing all the even number or all the odd number without considering that a number can be odd with a even power or vice-versa

check this

```
>>> math.log(170**3,3)
14.02441559235585
>>>
```

not even close, the correct method is described here Nth root

which is:

let x be the number to calculate the Nth root, n said root and r the result, then we get

r^{n} = x

take the log in any base from both sides, and solve for r

log_{b}( r^{n} ) = log_{b}( x )

n * log_{b}( r ) = log_{b}( x )

log_{b}( r ) = log_{b}( x ) / n

b^{logb( r ) } = b^{logb( x ) / n }

r = b^{logb( x ) / n }

so for instance with log in base 10 with get

```
>>> pow(10, math.log10(170**3)/3 )
169.9999999999999
>>>
```

that is much more closer, and with just rounding it we get the answer

```
>>> round(169.9999999999999)
170
>>>
```

therefore the function should be something like this

```
import math
def isPP(x):
for n in range(2, 1+round(math.log2(x)) ):
root = pow( 10, math.log10(x)/n )
result = round(root)
if result**n == x:
return result,n
```

the upper limit in range is to avoid testing numbers that will certainly fail

test

```
>>> isPP(170**3)
(170, 3)
>>> isPP(6434856)
(186, 3)
>>> isPP(9**2)
(9, 2)
>>> isPP(23**8)
(279841, 2)
>>> isPP(279841)
(529, 2)
>>> isPP(529)
(23, 2)
>>>
```