# Stirlings formula for factorials

n! is very laborious to calculate for high values of n because it requires doing (n-1) individual multiplications. The following equation by the Scottish mathematician James Stirling (1692-1770), known as the Stirling formula, can be used instead to get a very close approximation:

An n gets larger the two limits rapidly approach each other. So a commonly quoted simplification of Stirling's formula is thus:

The equation is sometimes useful for ratios of factorials, as is often needed in probability calculations. However, in general, using a gamma function is easier and more accurate:

So Excel's GAMMALN(x+1) = Log(x!), and calculating x!/y! is:

EXP(GAMMALN(x+1) - GAMMALN(y+1))

The ratio of the factorials is done in logs which means you can handle much higher numbers. For example, using FACT(1002)/FACT(999) to calculate 1002! / 999! will generate an error as Excel tries to calculate both large factorials. However, EXP(GAMMALN(1003)- GAMMALN(1000)) gives the value 1003001999.99923 which compares well against the true answer of 1000*1001*1002 = 1003002000.

Read on: Probability mass function (pmf)