我一直在研究这段代码,以找到 A 的最大指数可分为 N!但它似乎没有用。我已经到了可以找到不同因素的地步,但我只是返回了相同的空数组。我做错了什么?
import math
def highestPower(N,A):
factors = []
x = math.factorial(N)
for i in range(0,N,-1):
if x % A**i == 0:
factors += [i]
return factors
最佳答案
避免计算幂,摆脱 for
循环,尽可能多地除法。
import math
def highestPower(N,A):
result = 0
x = math.factorial(N)
while x % A == 0:
x /= A
result += 1
return result
您还可以避免使用从 1 到 N 的循环计算阶乘。
关于python - 求 A 的最高阶除以 N 的阶乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36296033/