---
title: "Stirling's formula for factorials"
canonical: "https://modelassist.epixanalytics.com/space/EA/26576582/Stirling's%20formula%20for%20factorials"
format: markdown
---
*n!* is very laborious to calculate for high values of *n* because it requires doing *(n-1)* individual multiplications. The following equation [by a Scots mathematician James Stirling (1692-1770)], known as the Stirling formula, can be used instead to get a very close approximation:

![image](media://712db398-8cfb-4c9c-8187-b17908580b0b)

> Macro (mathblock)


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

![image](media://641b97c6-ca57-47f0-9738-86856262252e)

> Macro (mathblock)


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

![image](media://02f2228c-7ee1-4066-be90-9afcf5dba5b2)

> Macro (mathblock)


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.


---