# Re: st: large numbers in comb(n,k) function: no success

 From wgould@stata.com (William Gould, StataCorp LP) To statalist@hsphsun2.harvard.edu Subject Re: st: large numbers in comb(n,k) function: no success Date Thu, 05 Feb 2009 09:13:33 -0600

```Inna Becher <inna.becher@uni-konstanz.de> wrote,

> I would like to implement a comb(n,k) function. But my Stata does not
> allow it because of large n, k-numbers. N=180000 and k=2000. Is there
> any other way to do it? I wasn't successful by using exp(lnfactorial(n))
> in mata as well.

Maarten buis <maartenbuis@yahoo.co.uk> replied,

> The outcome of comb(180000,2000) is going to be,
> ridiculously large (> 8e+307) and it hits the limit of what
> can be stored in double precision [...]

Yes, that's right.  In fact, the answer is between 1e+4770 and 1e+4771.

comb(n, k) is defined

n!
comb(n, k)  =  ---------   =  (n!)/( k! (n-k)! )
k! (n-k)!

Thus,

ln(comb(n,k)    =  ln( (n!)/( k! (n-k)! ) )
=  ln(n!) - ln(k!) - ln(n-k)!

Stata has a lnfactorial() function, so we can plug in and get

. scalar n = 180000

. scalar k = 2000

. display lnfactorial(n) - lnfactorial(k) - lnfactorial(n-k)
10983.753

In log base 10, that 10983.753/ln(10) = 4770.1833.  Hence my statement,
the answer is between 1e+4770 and 1e+4771.

1e+4770 is unimagineably big.  The number of particles in the observable
universe is estimated to be between 1e+72 and 1e+87, so it would not be
possible to tally 1e+4770.

-- Bill
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```