-3

I am trying to determine whether an integer can be expressed in the form of a^b where b>1 and the given integer is greater than 0. My code runs fine for all test cases except for one. It gives wrong answer for the following input: 536870912 I cant understand why.

 def isPower(self, A):
    if(A==1):
        return (1)
    for i in range(2,int(A**0.5)+1):
        val=log(A,i)
        if(int(val)-val==0):
            return (1)
    else:
        return (0)

1 Answers1

0

This will solve:

from  math import log
def isPower(A):
    if(A==1):
        return (1)
    if (A&-A)==(A):
        return (1)        
    for i in range(2,int(A**0.5)+1):
        val=log(A,i)
        if(int(val)-val==0):
            return (1)
    else:
        return (0)

If you are looking for reason on why it is not working please refer the below link:

Dealing with accuracy in Python math operations

Rahul Agarwal
  • 4,034
  • 7
  • 27
  • 51