w****r 发帖数: 28 | 3 试着写了个python的, n = 2000 花时间大概十几秒钟
def tolist(n):
result = []
while (n >= 10):
result.append(n % 10)
n = n / 10
result.append(n)
return result
def mult(num1, num2):
result = [0]*(len(num1) + len(num2))
for i in range(len(num1)):
for j in range(len(num2)):
temp = num1[i] * num2[j]
if temp >= 10:
result[i + j] += temp % 10
result[i + j + 1] += temp / 10
else:
result[i + j] += temp
for k in range(len(result)):
if result[k] >= 10:
result[k + 1] += result[k] / 10
result[k] = result[k] % 10
if result[-1] == 0:
result.pop(-1)
return result
def factsum(n):
result = [1]
for i in range(1, n + 1):
result = mult(result, tolist(i))
return sum(result)
|