3.1 page 89
\[\begin{equation} \sqrt{2πn} (\frac {n}{e})^ne^{1/(12n+1)}<n!<\sqrt{2πn} (\frac {n}{e})^ne^{1/(12n)} \end{equation}\]
Write a computer program to illustrate this inequality for n = 1 to 9.
fact <- function(n) {
if (n == 0) {
return(1)
} else {
return(n * fact(n-1))
}
}
for (n in 1:9) {
lower_bound <- sqrt(2 * pi * n) * (n / exp(1))^n * exp(1 / (12 * n + 1))
upper_bound <- sqrt(2 * pi * n) * (n / exp(1))^n * exp(1 / (12 * n))
exact_value <- fact(n)
cat("n =", n, "\n")
cat("Lower bound:", lower_bound, "\n")
cat("Exact value:", exact_value, "\n")
cat("Upper bound:", upper_bound, "\n")
cat("\n")
}
## n = 1
## Lower bound: 0.9958702
## Exact value: 1
## Upper bound: 1.002274
##
## n = 2
## Lower bound: 1.99732
## Exact value: 2
## Upper bound: 2.000652
##
## n = 3
## Lower bound: 5.996096
## Exact value: 6
## Upper bound: 6.000599
##
## n = 4
## Lower bound: 23.99082
## Exact value: 24
## Upper bound: 24.00102
##
## n = 5
## Lower bound: 119.9699
## Exact value: 120
## Upper bound: 120.0026
##
## n = 6
## Lower bound: 719.8722
## Exact value: 720
## Upper bound: 720.0092
##
## n = 7
## Lower bound: 5039.335
## Exact value: 5040
## Upper bound: 5040.041
##
## n = 8
## Lower bound: 40315.89
## Exact value: 40320
## Upper bound: 40320.22
##
## n = 9
## Lower bound: 362850.6
## Exact value: 362880
## Upper bound: 362881.4
This code defines a function fact to calculate the factorial of a given number n, and then loops through values of n from 1 to 9. For each value of n, it calculates the lower and upper bounds of the inequality using the formula provided, as well as the exact value of n! using the fact function. It then prints out the values for comparison.