簡體   English   中英

如何在python中找到大量的階乘鏈?

[英]How can I find chain of factorial of large number in python?

如果數字是3!! 應該結果6! 就是720 此代碼適用於小整數,但會拋出錯誤Python int too large to convert to C long為大整數的情況。 需要幫忙。 謝謝

import math
n = raw_input()
a = n.count('!')
b= int(n.replace('!',''))
while(a!=0):
    fact =1*math.factorial(b)
    b = fact
    a-=1
print fact

如果math.factorial不支持這么大的數字,您可以自己做:

    fact = reduce(lambda x, y: x * y, [x+1 for x in range(b)])

    >>> 4!!
    620448401733239439360000

但是,@ interjay是正確的,math.factorial()應該在合理的時間內處理最多8個數字。 無論如何,該算法應該是正確的,因為它可能永遠也不會回來或對大量內存耗盡。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM