斯特林公式
n
!
=
2
π
n
(
n
e
)
n
n!=\sqrt{2\pi n}\left(\frac{n}{e} \right )^{n}
n!=2πn(en)n
证明:
ln
(
1
+
x
)
=
x
−
x
2
2
+
x
3
3
−
x
4
4
+
⋯
(
−
1
<
x
≤
1
)
⋯
(
1
)
\ln \left(1+x \right )=x-\frac{x^{2}}{2}+\frac{x^{3}}{3}-\frac{x^{4}}{4}+ \cdots \left(-1<x\le 1 \right )\cdots \left(1 \right )
ln(1+x)=x−2x2+3x3−4x4+⋯(−1<x≤1)⋯(1)
ln
(
1
−
x
)
=
−
x
−
x
2
2
−
x
3
3
−
x
4
4
−
⋯
(
−
1
≤
x
<
1
)
⋯
(
2
)
\ln \left(1-x \right )=-x-\frac{x^{2}}{2}-\frac{x^{3}}{3}-\frac{x^{4}}{4}- \cdots \left(-1\le x<1 \right )\cdots\left(2 \right )
ln(1−x)=−x−2x2−3x3−4x4−⋯(−1≤x<1)⋯(2)
(
1
)
−
(
2
)
ln
(
1
+
x
)
−
ln
(
1
−
x
)
=
ln
(
1
+
x
1
−
x
)
=
2
(
x
+
x
3
3
⋯
)
(
−
1
<
x
<
1
)
\left(1 \right )-\left(2 \right )\ln\left(1+x \right )-\ln\left(1-x \right )=\ln\left(\frac{1+x}{1-x} \right )=2\left(x+\frac{x^{3}}{3}\cdots \right )(-1<x<1)
(1)−(2)ln(1+x)−ln(1−x)=ln(1−x1+x)=2(x+3x3⋯)(−1<x<1)
设
x
=
1
2
n
+
1
x=\frac{1}{2n+1}
x=2n+11
n
≥
0
n\geq0
n≥0
2
n
+
1
≥
1
2n+1\geq 1
2n+1≥1
0
<
x
≤
1
0< x\leq 1
0<x≤1
1
+
x
1
−
x
=
n
+
1
n
ln
(
n
+
1
n
)
=
2
(
1
2
n
+
1
+
1
3
(
2
n
+
1
)
3
+
1
5
(
2
n
+
1
)
5
+
⋯
)
ln
(
n
+
1
n
)
2
n
+
1
2
=
1
+
1
3
(
2
n
+
1
)
2
+
1
5
(
2
n
+
1
)
4
+
⋯
1
<
ln
(
n
+
1
n
)
2
n
+
1
2
≤
1
+
1
3
(
1
(
2
n
+
1
)
2
+
1
(
2
n
+
1
)
4
⋯
)
\begin{aligned} \frac{1+x}{1-x}&=\frac{n+1}{n}\\ \ln\left(\frac{n+1}{n} \right )&=2\left(\frac{1}{2n+1}+\frac{1}{3\left(2n+1 \right )^{3}} +\frac{1}{5\left(2n+1 \right )^{5}}+\cdots \right )\\ \ln\left(\frac{n+1}{n} \right )\frac{2n+1}{2}&=1+\frac{1}{3\left(2n+1 \right )^{2}} +\frac{1}{5\left(2n+1 \right )^{4}}+\cdots \end{aligned} \\ 1<\ln\left(\frac{n+1}{n} \right )\frac{2n+1}{2}\leq 1+\frac{1}{3}\left(\frac{1}{\left ( 2n+1 \right )^{2}} +\frac{1}{\left ( 2n+1 \right )^{4}}\cdots \right )
1−x1+xln(nn+1)ln(nn+1)22n+1=nn+1=2(2n+11+3(2n+1)31+5(2n+1)51+⋯)=1+3(2n+1)21+5(2n+1)41+⋯1<ln(nn+1)22n+1≤1+31((2n+1)21+(2n+1)41⋯)
1
+
1
3
(
1
(
2
n
+
1
)
2
+
1
(
2
n
+
1
)
4
+
⋯
)
=
1
+
1
3
(
1
(
2
n
+
1
)
2
1
−
1
(
2
n
+
1
)
2
)
=
1
+
1
3
1
(
2
n
+
1
)
2
−
1
=
1
+
1
3
1
2
n
(
2
n
+
2
)
=
1
+
1
12
n
(
n
+
1
)
\begin{aligned} &\quad 1+\frac{1}{3}\left(\frac{1}{\left(2n+1 \right )^{2}}+\frac{1}{\left(2n+1 \right )^{4}}+\cdots \right )\\ &=1+\frac{1}{3}\left(\frac{\frac{1}{\left(2n+1 \right )^{2}}}{1-\frac{1}{\left(2n+1 \right )^{2}}} \right )\\ &=1+\frac{1}{3}\frac{1}{\left(2n+1 \right )^2-1}\\ &=1+\frac{1}{3}\;\frac{1}{2n\left(2n+2 \right )}\\ &=1+\frac{1}{12n\left(n+1 \right )} \end{aligned}
1+31((2n+1)21+(2n+1)41+⋯)=1+31(1−(2n+1)21(2n+1)21)=1+31(2n+1)2−11=1+312n(2n+2)1=1+12n(n+1)1
1
<
ln
(
n
+
1
n
)
2
n
+
1
2
≤
1
+
1
12
n
(
n
+
1
)
e
<
e
(
n
+
1
2
)
ln
(
1
+
1
n
)
≤
e
(
1
+
1
12
n
(
n
+
1
)
)
e
<
(
1
+
1
n
)
(
n
+
1
2
)
≤
e
(
1
+
1
12
n
(
n
+
1
)
)
\begin{aligned} 1<&\ln\left(\frac{n+1}{n} \right )\frac{2n+1}{2}&\leq&1+\frac{1}{12n\left(n+1 \right )}\\ e<&e^{\left(n+\frac{1}{2} \right )\ln\left(1+\frac{1}{n} \right )}&\leq& e^{\left( 1+\frac{1}{12n\left(n+1 \right )}\right )}\\ e<&\left(1+\frac{1}{n} \right )^{\left(n+\frac{1}{2} \right )}&\leq &e^{\left( 1+\frac{1}{12n\left(n+1 \right )}\right )} \end{aligned}
1<e<e<ln(nn+1)22n+1e(n+21)ln(1+n1)(1+n1)(n+21)≤≤≤1+12n(n+1)1e(1+12n(n+1)1)e(1+12n(n+1)1)
设
a
n
=
n
!
e
n
n
(
n
+
1
2
)
a_{n}=\frac{n!e^{n}}{n^{\left(n+\frac{1}{2} \right )}}
an=n(n+21)n!en
a
n
a
n
+
1
=
n
!
e
n
n
(
n
+
1
2
)
(
n
+
1
)
!
e
(
n
+
1
)
(
n
+
1
)
(
n
+
3
2
)
=
(
n
+
1
)
n
+
1
2
(
n
+
1
)
e
(
n
+
1
)
n
n
+
1
2
=
(
1
+
1
n
)
n
+
1
2
e
\begin{aligned} &\quad \frac{a_{n}}{a_{n+1}}\\ &=\frac{\frac{n!e^{n}}{n^{\left(n+\frac{1}{2} \right )}}}{\frac{\left(n+1 \right )!e^{\left(n+1 \right )}}{\left(n+1 \right )^{\left(n+\frac{3}{2} \right )}}}\\ &=\frac{\left(n+1 \right )^{n+\frac{1}{2}}\left(n+1 \right )}{e\left(n+1 \right )n^{n+\frac{1}{2}}}\\ &=\frac{\left (1+\frac{1}{n} \right )^{n+\frac{1}{2}}}{e} \end{aligned}
an+1an=(n+1)(n+23)(n+1)!e(n+1)n(n+21)n!en=e(n+1)nn+21(n+1)n+21(n+1)=e(1+n1)n+21
e
<
(
1
+
1
n
)
n
+
1
2
≤
e
(
1
+
1
12
n
(
n
+
1
)
)
1
<
(
1
+
1
n
)
n
+
1
2
e
≤
e
1
12
n
(
n
+
1
)
1
<
a
n
a
n
+
1
≤
e
1
12
n
(
n
+
1
)
0
<
a
n
+
1
<
a
n
\begin{aligned} e<&\left(1+\frac{1}{n} \right )^{n+\frac{1}{2}}&\leq& e^{\left(1+\frac{1}{12n\left(n+1 \right )} \right )}\\ 1<&\frac{\left(1+\frac{1}{n} \right )^{n+\frac{1}{2}}}{e}&\leq& e^{\frac{1}{12n\left(n+1 \right )} }\\ 1<&\frac{a_{n}}{a_{n+1}}&\leq &e^{\frac{1}{12n\left(n+1 \right )} }\\ 0<&a_{n+1}&<&a_{n}\\ \end{aligned}
e<1<1<0<(1+n1)n+21e(1+n1)n+21an+1anan+1≤≤≤<e(1+12n(n+1)1)e12n(n+1)1e12n(n+1)1an
{
a
n
}
\{an\}
{an}单调递减有下界
设
lim
n
→
+
∞
a
n
=
a
\lim\limits_{n\rightarrow +\infty}a_{n}=a
n→+∞liman=a
a
n
∗
e
−
1
12
n
<
a
n
+
1
∗
e
−
1
12
(
n
+
1
)
lim
n
→
+
∞
a
n
∗
e
−
1
12
n
=
a
a_{n}*e^{-\frac{1}{12n}}<a_{n+1}*e^{-\frac{1}{12\left(n+1 \right )}}\\ \lim\limits_{n\rightarrow +\infty}a_{n}*e^{-\frac{1}{12n}}=a
an∗e−12n1<an+1∗e−12(n+1)1n→+∞liman∗e−12n1=a
a
n
∗
e
−
1
12
n
{a_{n}*e^{-\frac{1}{12n}}}
an∗e−12n1单调递增有上界
a
n
∗
e
−
1
12
n
<
a
<
a
n
e
−
1
12
n
<
a
a
n
<
1
−
1
12
n
<
ln
(
a
a
n
)
<
0
0
<
ln
(
a
a
n
)
−
1
12
n
=
θ
<
1
\begin{aligned} a_{n}*e^{-\frac{1}{12n}}<&a&<&a_{n}\\ e^{-\frac{1}{12n}}<&\frac{a}{an}&<&1\\ -\frac{1}{12n}<&\ln\left(\frac{a}{an} \right )&<&0\\ 0<&\frac{\ln\left(\frac{a}{an} \right )}{-\frac{1}{12n}}=\theta &<&1 \end{aligned}
an∗e−12n1<e−12n1<−12n1<0<aanaln(ana)−12n1ln(ana)=θ<<<<an101
a
n
=
a
∗
e
θ
12
n
=
n
!
e
n
n
n
+
1
2
\begin{aligned} &\quad a_{n}\\ &=a*e^{\frac{\theta}{12n} }\\ &=\frac{n!e^{n}}{n^{n+\frac{1}{2}}} \end{aligned}
an=a∗e12nθ=nn+21n!en
n
!
=
a
∗
e
θ
12
n
∗
n
(
n
+
1
2
)
e
n
=
a
(
n
e
)
n
n
e
θ
12
n
\begin{aligned} &\quad n!\\ &=\frac{a*e^{\frac{\theta}{12n}}*n^{\left(n+\frac{1}{2} \right )}}{e^{n}}\\ &=a\left(\frac{n}{e} \right )^{n}\sqrt{n}\;e^{\frac{\theta}{12n}} \end{aligned}
n!=ena∗e12nθ∗n(n+21)=a(en)nne12nθ
由Wallis公式,
lim
n
→
+
∞
2
4
n
(
(
n
!
)
2
(
2
n
)
!
)
2
1
2
n
+
1
=
π
2
\lim\limits_{n\rightarrow+ \infty }2^{4n}\left ( \frac{\left ( n! \right )^{2}}{\left ( 2n \right)!} \right )^{2} \frac{1}{2n+1}=\frac{\pi }{2}
n→+∞lim24n((2n)!(n!)2)22n+11=2π
lim
n
→
+
∞
2
4
n
(
(
n
!
)
2
(
2
n
)
!
)
2
2
n
+
1
=
lim
n
→
+
∞
2
4
n
(
(
a
(
n
e
)
n
n
e
θ
12
n
)
2
a
(
2
n
e
)
2
n
2
n
e
θ
24
n
)
2
1
2
n
+
1
=
lim
n
→
+
∞
2
4
n
(
a
2
(
n
e
)
2
n
n
e
4
θ
24
n
a
(
2
n
e
)
2
n
2
n
e
θ
24
n
)
2
1
2
n
+
1
=
lim
n
→
+
∞
2
4
n
(
a
(
1
2
)
2
n
n
2
e
θ
8
n
)
2
1
2
n
+
1
=
lim
n
→
+
∞
2
4
n
a
2
2
−
4
n
n
2
e
θ
4
n
1
2
n
+
1
=
lim
n
→
+
∞
a
2
n
e
θ
4
n
2
(
2
n
+
1
)
=
a
2
4
=
π
2
\begin{aligned} &\quad \lim_{n\rightarrow +\infty}2^{4n}\frac{\left(\frac{\left ( n! \right )^2}{\left(2n \right )!} \right )^{2}}{2n+1}\\ &=\lim_{n\rightarrow +\infty}2^{4n}\left ( \frac{\left(a\left(\frac{n}{e} \right )^{n} \sqrt{n}\;e^{\frac{\theta }{12n}}\right )^{2}}{a\left(\frac{2n}{e} \right )^{2n} \sqrt{2n}\;e^{\frac{\theta }{24n}}} \right )^2\;\frac{1}{2n+1}\\ &=\lim_{n\rightarrow +\infty}2^{4n}\left ( \frac{a^{2}\left(\frac{n}{e} \right )^{2n} n\;e^{\frac{4\theta }{24n}}}{a\left(\frac{2n}{e} \right )^{2n} \sqrt{2n}\;e^{\frac{\theta }{24n}}} \right )^2\;\frac{1}{2n+1}\\ &=\lim_{n\rightarrow +\infty}2^{4n}\left ( a\left(\frac{1}{2} \right )^{2n}\frac{\sqrt{n}}{\sqrt{2}}e^{\frac{\theta }{8n}}\right )^2\;\frac{1}{2n+1}\\ &=\lim_{n\rightarrow +\infty}2^{4n}a^{2}2^{-4n}\frac{n}{2}e^{\frac{\theta}{4n} }\frac{1}{2n+1}\\ &=\lim_{n\rightarrow +\infty}\frac{a^{2}ne^{\frac{\theta}{4n}}}{2(2n+1)}\\ &=\frac{a^{2}}{4}\\ &=\frac{\pi}{2} \end{aligned}
n→+∞lim24n2n+1((2n)!(n!)2)2=n→+∞lim24n⎝⎜⎛a(e2n)2n2ne24nθ(a(en)nne12nθ)2⎠⎟⎞22n+11=n→+∞lim24n(a(e2n)2n2ne24nθa2(en)2nne24n4θ)22n+11=n→+∞lim24n(a(21)2n2ne8nθ)22n+11=n→+∞lim24na22−4n2ne4nθ2n+11=n→+∞lim2(2n+1)a2ne4nθ=4a2=2π
a
=
2
π
a=\sqrt{2\pi}
a=2π
n
!
=
a
(
n
e
)
n
n
e
θ
4
n
=
2
π
(
n
e
)
n
n
e
θ
4
n
(
0
<
θ
<
1
)
(
θ
=
ln
(
a
a
n
)
−
1
12
n
)
≈
2
π
n
(
n
e
)
n
\begin{aligned} &\quad n!\\ &=a\left(\frac{n}{e} \right )^{n}\sqrt{n}\;e^{\frac{\theta}{4n}}\\ &=\sqrt{2\pi}\left(\frac{n}{e} \right )^{n}\sqrt{n}\;e^{\frac{\theta}{4n}}\left(0<\theta<1 \right )\left(\theta=\frac{\ln\left(\frac{a}{a_{n}} \right )}{-\frac{1}{12n}} \right )\\ &\approx \sqrt{2\pi n}\left(\frac{n}{e} \right )^{n} \end{aligned}
n!=a(en)nne4nθ=2π(en)nne4nθ(0<θ<1)⎝⎛θ=−12n1ln(ana)⎠⎞≈2πn(en)n
51nod 1130
解法: n n n的位数= lg n + 1 \lg n+1 lgn+1,所以斯特林公式= 1 2 lg ( 2 π n ) + n ∗ lg ( π e ) \frac{1}{2}\lg(2πn)+n*\lg(\frac{\pi}{e}) 21lg(2πn)+n∗lg(eπ)
#include<iostream>
#include<cmath>
using namespace std;
const double PI = acos(-1);
const double E = exp(1);
const int getLength(int n) {
if (!n)
return 1;
int a = (log(2 * PI*n) / 2 + n*log(n / E)) / log(10);
return a + 1;
}
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
cout << getLength(n) << endl;
}
return 0;
}