Libraries
In this section, I will include all libraries needed.
libraries <- c(
"plotly",
"tidyverse",
"xtable"
)
9 † Page 89
Book: Grinstead: Introduction to Probability
Exercise
A more refined inequality for approximating \(n!\) is given by
\[\sqrt{2\pi n} \left( \frac{n}{e} \right)^n e^{1/(12n+1)} < n! < \sqrt{2\pi n} \left( \frac{n}{e} \right)^n e^{1/(12n)}\] Write a computer program to illustrate this inequality for n = 1 to 9.
Solution
- Left Side
factorial_left <- function(n){
n_row <- c()
fact_l <- c()
fact_l[1] <- 1
e <- exp(1)
for (i in 1:n){
n_row[i] <- i
fact_l[i] <- round(sqrt(2 * pi * i) * (i / e)^(i) * e^( 1 / (12 * i + 1) ),2)
}
fact_l <- data.frame('n' = n_row, 'n_left' =fact_l)
return(fact_l)
}
- Factorial
factorial_center <- function(n){
n_row <- c()
fact_c <- c()
nfac <- 1
e <- exp(1)
for (i in 1:n){
nfac <- nfac * i
n_row[i] <- i
fact_c[i] <- nfac
}
fact_c <- data.frame('n' = n_row, 'n_center' =fact_c)
return(fact_c)
}
- Right Side
factorial_right <- function(n){
n_row <- c()
fact_r <- c()
fact_r[1] <- 1
e <- exp(1)
for (i in 1:n){
n_row[i] <- i
fact_r[i] <- round(sqrt(2 * pi * i) * (i / e)^(i) * e^( 1 / (12 * i ) ),2)
}
fact_r <- data.frame('n' = n_row, 'n_right' =fact_r)
return(fact_r)
}
Table
Even though is not very clear in the plot, by looking at the results, we can notice that yes, in fact these approximations illustrate the inequality!
n | left side | n! | right side |
---|---|---|---|
1 | 1.00 | 1.00 | 1.00 |
2 | 2.00 | 2.00 | 2.00 |
3 | 6.00 | 6.00 | 6.00 |
4 | 23.99 | 24.00 | 24.00 |
5 | 119.97 | 120.00 | 120.00 |
6 | 719.87 | 720.00 | 720.01 |
7 | 5039.33 | 5040.00 | 5040.04 |
8 | 40315.89 | 40320.00 | 40320.22 |
9 | 362850.55 | 362880.00 | 362881.38 |