【发布时间】:2017-10-10 05:34:35
【问题描述】:
我需要帮助解决以下问题。
给定一个整数m,我需要找到正整数n的个数和整数,使得n的阶乘以正好结束> m 零。
我编写了这段代码,它工作正常并且我得到了正确的输出,但是随着数字的增加,它花费了太多时间。
a = input()
while a:
x = []
m, n, fact, c, j = input(), 0, 1, 0, 0
z = 10*m
t = 10**m
while z - 1:
fact = 1
n = n + 1
for i in range(1, n + 1):
fact = fact * i
if fact % t == 0 and ((fact / t) % 10) != 0:
x.append(int(n))
c = c + 1
z = z - 1
for p in range(c):
print x[p],
a -= 1
print c
有人可以建议我一种更有效的方法来做到这一点。目前,一个测试用例要求在其阶乘中以 250 尾随零的数字需要 30 秒。
谢谢
【问题讨论】:
标签: algorithm python-2.7 factorial trailing