On Mar 20, 12:38 am, gracia <graciaw...@gmail.comwrote:

I need to compute the value of binomial(n, k)=n!/k!(n-k)! *

(1-p)^n * p^k.

When n and k is very big (e.g. n 160), the step to compute

n!/k!(n- k)! is always overflow even when I used unsigned long

long.

Can anyone help me?

If you have access to the C99 math functions (which will be part

of the next version of C++ as well), you can use something like:

double

fact(

double i )

{

return round( exp( gamma( (double)( i + 1 ) ) ) ) ;

}

long

binom(

long i,

long j )

{

return round( fact( i ) / ( fact( j ) * fact( i - j ) ) ) ;

}

On the other hand, I'm not sure that it will be really correct

for very large values. Typically, a double will not be able to

represent all of the needed values exactly.

--

James Kanze (GABI Software) email:ja*********@gmail.com

Conseils en informatique orientï¿½e objet/

Beratung in objektorientierter Datenverarbeitung

9 place Sï¿½mard, 78210 St.-Cyr-l'ï¿½cole, France, +33 (0)1 30 23 00 34