# mathlib3documentation

analysis.special_functions.stirling

# Stirling's formula #

THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.

This file proves Stirling's formula for the factorial. It states that $n!$ grows asymptotically like $\sqrt{2\pi n}(\frac{n}{e})^n$.

## Proof outline #

The proof follows: https://proofwiki.org/wiki/Stirling%27s_Formula.

We proceed in two parts.

Part 1: We consider the sequence $a_n$ of fractions $\frac{n!}{\sqrt{2n}(\frac{n}{e})^n}$ and prove that this sequence converges to a real, positive number $a$. For this the two main ingredients are

• taking the logarithm of the sequence and
• using the series expansion of $\log(1 + x)$.

Part 2: We use the fact that the series defined in part 1 converges againt a real number $a$ and prove that $a = \sqrt{\pi}$. Here the main ingredient is the convergence of Wallis' product formula for π.

### Part 1 #

https://proofwiki.org/wiki/Stirling%27s_Formula#Part_1

noncomputable def stirling.stirling_seq (n : ) :

Define stirling_seq n as $\frac{n!}{\sqrt{2n}(\frac{n}{e})^n}$. Stirling's formula states that this sequence has limit $\sqrt(π)$.

Equations
@[simp]
@[simp]
theorem stirling.log_stirling_seq_formula (n : ) :
= - 1 / 2 * real.log (2 * (n.succ)) - (n.succ) * real.log ((n.succ) / rexp 1)

We have the expression log (stirling_seq (n + 1)) = log(n + 1)! - 1 / 2 * log(2 * n) - n * log ((n + 1) / e).

theorem stirling.log_stirling_seq_diff_has_sum (m : ) :
has_sum (λ (k : ), 1 / (2 * (k.succ) + 1) * ((1 / (2 * (m.succ) + 1)) ^ 2) ^ k.succ)

The sequence log (stirling_seq (m + 1)) - log (stirling_seq (m + 2)) has the series expansion ∑ 1 / (2 * (k + 1) + 1) * (1 / 2 * (m + 1) + 1)^(2 * (k + 1))

The sequence log ∘ stirling_seq ∘ succ is monotone decreasing

theorem stirling.log_stirling_seq_diff_le_geo_sum (n : ) :
(1 / (2 * (n.succ) + 1)) ^ 2 / (1 - (1 / (2 * (n.succ) + 1)) ^ 2)

We have a bound for successive elements in the sequence log (stirling_seq k).

We have the bound log (stirling_seq n) - log (stirling_seq (n+1)) ≤ 1/(4 n^2)

For any n, we have log_stirling_seq 1 - log_stirling_seq n ≤ 1/4 * ∑' 1/k^2

The sequence log_stirling_seq is bounded below for n ≥ 1.

theorem stirling.stirling_seq'_pos (n : ) :

The sequence stirling_seq is positive for n > 0

The sequence stirling_seq has a positive lower bound.

The sequence stirling_seq ∘ succ is monotone decreasing

The limit a of the sequence stirling_seq satisfies 0 < a

### Part 2 #

https://proofwiki.org/wiki/Stirling%27s_Formula#Part_2

The sequence n / (2 * n + 1) tends to 1/2

For any n ≠ 0, we have the identity (stirling_seq n)^4 / (stirling_seq (2*n))^2 * (n / (2 * n + 1)) = W n, where W n is the n-th partial product of Wallis' formula for π / 2.

theorem stirling.second_wallis_limit (a : ) (hane : a 0)  :
(nhds (a ^ 2 / 2))

Suppose the sequence stirling_seq (defined above) has the limit a ≠ 0. Then the Wallis sequence W n has limit a^2 / 2.