Stirling's Approximation for $\ln n!$ is:
$$\ln n! \approx n \ln n - n.$$
Write a program to output $\ln n!$, its Stirling's approximation and the relative error in the approximation, (exact-approx)/exact, for $n < 100$. Then modify the program to output only the first value of $n$ for which the relative error is less than 1%. Hint: the math
module provides ("exposes") a factorial()
method.
This program outputs $\ln n!$, its Stirling's approximation and the relative error in the approximation for $n < 100$
import math
for n in range(2,100):
e = math.log(math.factorial(n))
s = n * math.log(n) - n
err = (e - s) / e
print(e, s, err)
To output just the first value of $n$ for which the relative error is less that 1%:
import math
for n in range(2,100):
e = math.log(math.factorial(n))
s = n * math.log(n) - n
err = (e - s) / e
if err < 0.01:
print(e, s, err)
break