is_perfect is a method to check whether a number has a perfect nth root.
For example:
- is_perfect(125,3) should return True as 5^3 is 125 an integer
- is_perfect(126,3) should return False as there is no integer M for which M^3 is an integer
def is_perfect(num,power):
root = 0.00
p = 0.00
float(p)
p = 1.0/power
root = num**(p)
print ("root",root,sep = ' ')
print ("root**power",root**power,sep = ' ')
check = num -(root**power)
print (check)
if check < 1e-14:
root = math.ceil(root)
if (root-int(root)) ==0:
print(num,root,int(root),p,sep = ' ')
return True
else:
print(num,root,int(root),p,sep=' ')
return False
In Python shell both give False when the result of 125 should be true.
>>> is_perfect(126,3)
root 5.0132979349645845
root**power 125.99999999999999
1.4210854715202004e-14
126 5.0132979349645845 5 0.3333333333333333
False
>>> is_perfect(125,3)
root 4.999999999999999
root**power 124.99999999999993
7.105427357601002e-14
125 4.999999999999999 4 0.3333333333333333
False
>>>
How can I modify my method to achieve the desired result.