Python 2.7 关于指数的查询

Python 2.7 关于指数的查询,python-2.7,exponent,Python 2.7,Exponent,检查数字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 ver

检查数字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
def检查(n): #对数字求和,并根据和取输入的对数 l=math.log(n,sum(int(e)表示str(n)中的e))) #如果差异非常小,则可以表示为是 返回l-int(l)<1e-6,int(l)#如果只需要检查,则跳过第二步 检查(4)#正确,1 检查(512)#正确,3 检查(511)#错误,3
导入数学
def检查(n):
#对数字求和,并根据和取输入的对数
l=math.log(n,sum(int(e)表示str(n)中的e)))
#如果差异非常小,则可以表示为是
返回l-int(l)<1e-6,int(l)#如果只需要检查,则跳过第二步
检查(4)#正确,1
检查(512)#正确,3
检查(511)#错误,3

也许有一种数学方法,但蛮力法在这里看起来足够了:计算
x
(这里5+1+2=8)的数字总和,并尝试所有可能的指数,直到结果大于或等于
x
(8^1,8^2,8^3)。也许有一种数学方法,但是强力法在这里看起来已经足够了:计算
x
(这里是5+1+2=8)的数字总和,并尝试所有可能的指数,直到结果大于或等于
x
(8^1,8^2,8^3)。