关于指数的查询
Query about exponents
检查数字 x 是否可以表示为 x 的 y 次方数字之和的好方法。
例如,512 有效,因为 5 + 1 + 2 = 8,而 8^3 = 512。
我只需要一般方法的帮助,而不是代码。
谢谢!
import math
def check(n):
# sum digits and take the logarithm of input according to sum
l = math.log(n, sum(int(e) for e in str(n)))
# if diff is very small, then yes it can be expressed
return l - int(l) < 1e-6, int(l) # skip second if only check is needed
check(4) # True, 1
check(512) # True, 3
check(511) # False, 3
检查数字 x 是否可以表示为 x 的 y 次方数字之和的好方法。
例如,512 有效,因为 5 + 1 + 2 = 8,而 8^3 = 512。 我只需要一般方法的帮助,而不是代码。
谢谢!
import math
def check(n):
# sum digits and take the logarithm of input according to sum
l = math.log(n, sum(int(e) for e in str(n)))
# if diff is very small, then yes it can be expressed
return l - int(l) < 1e-6, int(l) # skip second if only check is needed
check(4) # True, 1
check(512) # True, 3
check(511) # False, 3