is_perfect は、数値に完全な n 乗根があるかどうかを調べるメソッドです。
例:
- is_perfect(125,3)は、5^3 が 125 の整数であるため、Trueを返す必要があります
- is_perfect(126,3)は、M^3 が整数である整数 M がないため、Falseを返す必要があります。
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
Python シェルでは、125 の結果が true になるはずのときに、どちらも False を返します。
>>> 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
>>>
メソッドを変更して目的の結果を得るにはどうすればよいですか。