Python计算阶乘的尾随零
正在编写一个示例,但它不起作用。必须有一个函数在n中计算尾随的零!阶乘公式。其中,n是我们要为其阶乘查找尾随零数的数字。使用了一些进口产品,但不起作用 我的代码:Python计算阶乘的尾随零,python,Python,正在编写一个示例,但它不起作用。必须有一个函数在n中计算尾随的零!阶乘公式。其中,n是我们要为其阶乘查找尾随零数的数字。使用了一些进口产品,但不起作用 我的代码: def is_positive_integer(x): try: x = float(x) except ValueError: return False else: if x.is_integer() and x > 0: retu
def is_positive_integer(x):
try:
x = float(x)
except ValueError:
return False
else:
if x.is_integer() and x > 0:
return True
else:
return False
def trailing_zeros(num):
if is_positive_integer(num):
# The above function call has done all the sanity checks for us
# so we can just convert this into an integer here
num = int(num)
k = math.floor(math.log(num, 5))
zeros = 0
for i in range(1, k + 1):
zeros = zeros + math.floor(num/math.pow(5, i))
return zeros
else:
print("Factorial of a non-positive non-integer is undefined")
例:
输出必须是:
Trailing 0s in n! = Count of 5s in prime factors of n!
= floor(n/5) + floor(n/25) + floor(n/125) + ....
这段代码可以完成这项工作,您的代码非常复杂
def Zeros(n):
count = 0
i = 5
while n / i >= 1:
count += int(n / i)
i *= 5
return int(count)
n = 100
print("Trailing zeros " +
"in 100! is", Zeros(n))
输出:
Trailing zeros in 100! is 24
您显示的代码有什么问题?难道输出的内容看起来不像是应该的吗?我正在寻找一个更干净的代码,请编辑你的帖子并提出一个具体的问题?目前还不清楚你到底在问什么。好吧,我知道答案了
Trailing zeros in 100! is 24