Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Calculate the sum of large numbers on Python

I use this code:

import scipy.misc
import math
from decimal import Decimal
    
def n_choose_k(N, k, N2, k2, s):
    if k > N:
        return 0
        a1 = Decimal.from_float(math.factorial(N))
        a2 = Decimal.from_float(math.factorial(k))
        a3 = Decimal.from_float(math.factorial(N-k))
        a4 = Decimal.from_float(math.factorial(N2))
        a5 = Decimal.from_float(math.factorial(k2))
        a6 = Decimal.from_float(math.factorial(N2-k2))
        a7 = Decimal.from_float(s)
        rez = Decimal.from_float(0)
        rez = (-1)**a7 * (a1/(a2*a3)) * (a4/(a5*a6))
        return rez
                   
accum = 0
    
for s in range(0, 101):
    accum += n_choose_k(30+s,s,100,100-s,s)

Answer:

-10148107944337512107223500

But it is wrong answer.

If i use scipy.misc.comb, then answer: 7075575608218126046195672175200960512 Ii's very right answer, but not presicion.

Please help.

question from:https://stackoverflow.com/questions/66058590/calculate-the-sum-of-large-numbers-on-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...