# Stirling approximation for factorials

## [Part 2] Deriving the Stirling formula.

In the previous post, we introduced some properties of infinite series and how to test their convergence. In this post we will try to derive the Stirling formula. But before we do that, we will first introduce 2 more properties for testing series convergence, and the Wallis formula, which will be used for the proof.

## Limit Comparison test

Let and two series, where .

If

Then

**Proof**

Let such that

It follows then

Whihch means that

Hence

**N.B**

From this property we can derive the following, if is a series and such that

If and

Then converges.

The proof is quite simple if we consider the series , we have

## The ratio rule, D’Alembert’s rule

Let a series with the general term strictly positive, and , with

- i) if the series converges
- ii) if the series diverges

**Proof**

- i) Let

We know that

Then

Which means that

And since , we can conclude the convergence using the previous property.

- ii) and

This means that there’s a certain integer after which , which means diverges.

**N.B**

If , we can’t say anything about the convergence of the series.

As an example, let’s consider

We have

And we know that the series converges for and diverges for .

## Wallis formula

Let’s consider, Wallis integral,

This integral verifies the following properties:

- strictly positive and decreasing
- for

We will try these properties by induction

- case ,
- case ,
- case ,

- We will assume that is strictly positive and decreasing, and we will prove that the property holds for

Hence and , and .

By applying the result for and , we get

Which gives us the Wallis formula

## Stirling formula

Let’s consider the following series

If we apply the ratio test to this series

- if , the series converges
- if , the series dieverges
- if , we have

Ans since

It follows that

Which means that

Hence converges.

Now we only need to determine the value of , and for this we will use the Wallis formula,

Also,

The result is amazing, because it replaces the factorial function which is complicated with a simple expression. Also for very large numbers, the factorial is an expensive function, on the other hand the approximation is less computationally intensive.

```
1 from sympy.mpmath import e, sqrt, pi
2
3 def stirling(n):
4 return sqrt(2 * pi * n) * (n / e) ** n
5
6
7 for n in xrange(1, 10):
8 print 'n:{} \t factorial: {} \t stirling: {}'.format(n, math.factorial(n), stirling(n))
9
10
11 # n:1 factorial: 1 stirling: 0.922137008895789
12 # n:2 factorial: 2 stirling: 1.91900435148898
13 # n:3 factorial: 6 stirling: 5.83620959134586
14 # n:4 factorial: 24 stirling: 23.5061751328933
15 # n:5 factorial: 120 stirling: 118.01916795759
16 # n:6 factorial: 720 stirling: 710.078184642185
17 # n:7 factorial: 5040 stirling: 4980.39583161246
18 # n:8 factorial: 40320 stirling: 39902.3954526567
19 # n:9 factorial: 362880 stirling: 359536.872841948
20
21
22 # approximation for a large value
23
24 stirling(10**10)
25 # 2.3257959759770513e+95657055186
```

Also based on this formula we can find easily the following result:

In the next post we will try to approximate the factorials using the Euler gamma function.