[問題] 看似等價但出錯的code求解
# fact(n):= n! = n*(n-1)*...*2*1
def fact(n):
total = 1
while n >= 1:
total *= n
n -= 1
return total
#comb(n,m):= C(n,m) = fact(n)/(fact(m)*fact(n-m))
def comb(n,m):
return fact(n)/(fact(m)*fact(n-m))
#simply define a function f as f(n):= comb(n,n/2)/2**n for even n
def f(n):
return comb(n,n/2) / 2**n
print( f(200) ) # OverflowError: int too large to convert to float
print( comb(200,100) / 2**200 ) # 0.05634..., it works!
------------------------------------------------
也就是說,上面code的f(200)跟comb(200,100) / 2**200 幾乎等價
只差在有沒有包成function,但是一個可以run一個卻說正整數太大
除非python在定義函數是是走另外一條路??
這問題發生時覺得好奇怪完全不知道怎麼google QQ
謝謝幫忙!
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 219.68.160.241
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1523693794.A.A6D.html
推
04/14 17:07,
7年前
, 1F
04/14 17:07, 1F
→
04/14 17:07,
7年前
, 2F
04/14 17:07, 2F
→
04/14 18:06,
7年前
, 3F
04/14 18:06, 3F
推
04/15 10:41,
7年前
, 4F
04/15 10:41, 4F
→
04/15 10:41,
7年前
, 5F
04/15 10:41, 5F
→
04/15 10:41,
7年前
, 6F
04/15 10:41, 6F
→
04/15 17:29,
7年前
, 7F
04/15 17:29, 7F
→
04/15 17:29,
7年前
, 8F
04/15 17:29, 8F
→
04/16 08:20,
7年前
, 9F
04/16 08:20, 9F
Python 近期熱門文章
PTT數位生活區 即時熱門文章