I am a beginner who is currently studying Rails on my own. As a hobby, I use Python for numerical calculations. This time, when $ n $ is infinite, the geometric mean of $ n! $, That is,
\lim_{n \to \infty} \frac{\sqrt[n]{n!}}{n}\\
I would like to somehow ask for it in Python.
Speaking of the average, you can think of the ** arithmetic mean **, which is the sum of all $ n $ data $ x_1, x_2, ..., x_n $ and divided by $ n $. On the other hand, the geometric mean $ \ mu $ is
\mu = \sqrt[n]{x_1x_2...x_n}\\
You can find it at. It is often used to calculate growth rates and interest rates.
Now, let's find the geometric mean of n! Shown above. Using the above formula, it can be calculated by $ \ sqrt [n] {n!} $, But it seems that it will not converge when it is skipped to infinity, so consider the case where the one divided by $ n $ is skipped to infinity. .. In other words, find out what percentage of the total $ \ mu $ is. Therefore
\lim_{n \to \infty} \frac{\sqrt[n]{n!}}{n}\\
I will ask for.
First of all, I would like to plot a graph using matplotlib to see which value is likely to converge by trying to change the number to $ n $. With sympy, you can easily calculate even $ 2000! $. If you use the standard module math, you will get an error if the number of digits is too large.
plot.py
import matplotlib.pyplot as plt
import sympy
def fun(n):
n1 = sympy.factorial(n)
n2 = n1 ** (1/n)
n3 = n2/n
return n3
x = list(range(1,2000,10))
y = []
for i in x:
y.append(fun(i))
print(x)
print(y)
plt.plot(x,y)
plt.ylim([0.36,0.4])
Looking at the graph, I feel that it seems to converge around 0.37. Now suppose the above equation converges to some value $ a $. Also, it is difficult to solve with the above formula, so if you take the logarithm,
\log \lim_{n \to \infty} \frac{\sqrt[n]{n!}}{n} = \log a\\
\lim_{n \to \infty} \log \frac{\sqrt[n]{n!}}{n} = \log a\\
To organize,
\begin{align}
\lim_{n \to \infty} \log \frac{\sqrt[n]{n!}}{n} &= \lim_{n \to \infty} (\log {\sqrt[n]{n!}}-\log {n})\\
&= \lim_{n \to \infty} \Bigl( \frac{1}{n}\log {n!}-\log {n}\Bigr)\\
&= \lim_{n \to \infty} \Bigl( \frac{1}{n}\sum_{k=1}^{n} \log k-\log {n}\Bigr)
\end{align}\\
here
\sum_{k=1}^{n} \log k
I will think about. It can be expected that the Squeeze theorem can be used when the limit of the division series comes out. here
\begin{align}
f(x) &= \log({x-1})\\
g(x) &= \log {x}
\end{align}
Introduce. The reason will be shown below. Plot the above three formulas again using matplotlib.
plot2.py
with np.errstate(invalid='ignore'):
x = np.arange(-1, 11, 0.01)
y1 = np.log(x)
y2 = np.log(x-1)
fig = plt.figure()
plt.xlim([-1, 11])
plt.ylim([-2, 4])
plt.xlabel('x')
plt.ylabel('y', rotation=0)
plt.gca().set_aspect('equal')
plt.grid()
plt.plot(x, y1, label="g(x)")
plt.plot(x, y2, label="f(x)", color="green")
x = list(range(1,11,1))
y3 = []
for i in x:
y3.append(np.log(i))
print(y3)
plt.bar(x, y3, width = 1.0, align="edge",color="orange", edgecolor="black", label="Σlogk")
plt.legend(bbox_to_anchor=(1, 1), loc='upper right', borderaxespad=0, fontsize=8)
plt.show()
fig.savefig("log.jpg ")
As you can see from the plot, $ \ sum_ {k = 1} ^ {n} \ log k $ is between the integrals of $ f (x) $ and $ g (x) $. Therefore,
\int_{2}^{n}\log({x-1})dx< \sum_{k=1}^{n} \log k < \int_{2}^{n}\log {x} dx\\
The definite integral of the above logarithmic function can be solved by using integration by parts. But here the purpose is to use Python, so
integral.py
import sympy as sym
from sympy.plotting import plot
sym.init_printing(use_unicode=True)
from sympy import log
n, x, y = sym.symbols("n x y")
logx1 = log(x)
logx2 = log(x-1)
q1 = sym.integrate(logx1, (x, 2, n+1))
q2 = sym.integrate(logx2, (x, 2, n+1))
print(q1)
print(q2)
-n + (n + 1)*log(n + 1) - 2*log(2) + 1 #log(x)Definite integral solution
-n + (n + 1)*log(n) - log(n) + 1 #log(x-1)Definite integral solution
Then divide each by $ n $ and subtract $ \ log {n} $. In other words, fix it to the first formula.
\frac{1}{n}-1< \frac{1}{n}\sum_{k=1}^{n} \log k-\log {n} < \log \Bigl(1+\frac{1}{n}\Bigr)+\frac{1}{n}\log(n+1)+\frac{1}{n}-\frac{2}{n}\log2\\
The limit is also found using sympy.
integral.py
import sympy as sym
from sympy.plotting import plot
sym.init_printing(use_unicode=True)
from sympy import log
n, x, y = sym.symbols("n x y")
logx1 = log(x)
logx2 = log(x-1)
q1 = sym.integrate(logx1, (x, 2, n+1))
q2 = sym.integrate(logx2, (x, 2, n+1))
q11 = q1/n-log(n)
q22 = q2/n-log(n)
oo = sympy.oo
lim_q11 = sym.limit(q11, n, oo)
lim_q22 = sym.limit(q22, n, oo)
print(lim_q11)
print(lim_q22)
-1
-1
Therefore, from Squeeze theorem
\lim_{n \to \infty} \Bigl( \frac{1}{n}\sum_{k=1}^{n} \log k-\log {n}\Bigr)=\lim_{n \to \infty} \log \frac{\sqrt[n]{n!}}{n} = -1
\\
By the way, I set the convergence value to $ a $.
\lim_{n \to \infty} \log \frac{\sqrt[n]{n!}}{n} = \log a\\
\begin{align}
a&= \lim_{n \to \infty} \frac{\sqrt[n]{n!}}{n}\\
&= \frac{1}{e}\\
&= 0.367...
\end{align}\\
Thank you for your hard work. Finally the convergence came out. First, it matches the predicted convergence value by substituting an appropriate number. It is a beautiful solution that gives the natural logarithm $ e $.
How was it? I solved it using a module for numerical calculation in Python, but I think there is a smarter way to solve it. This problem can also be solved within the scope of high school mathematics. It may be fun to think of another solution.
Understanding integrals in Python Solving "extreme value" related problems in high school mathematics with Python
Recommended Posts