A more refined inequality for approximating \(n!\) is given by
\[\sqrt{2\pi n}(\frac{n}{e})^ne^{\frac{1}{12n+1}}<n!< \sqrt{2\pi n}(\frac{n}{e})^ne^{\frac{1}{12n}}\]
Write a computer program to illustrate this inequality for \(n\) = 1 to 9.
First we will define our function using the above principle:
approxFactorial <- function(n){
for(i in 1:n){
lowerBound = sqrt(2*pi*i)* ((i/exp(1))^i)* exp(1)^(1/((12*i) + 1))
upperBound = sqrt(2*pi*i)* ((i/exp(1))^i)* exp(1)^(1/(12*i))
print(paste0('n = ',i))
print(paste0('The approximation of n! is between: ',lowerBound,' and ',upperBound))
print(paste0('The true value of n! is:',factorial(i)))
print(paste0('n! is between the lower and upper bounds of the approximation: ',lowerBound < factorial(i) & factorial(i) < upperBound))
print('---------------------')
}
}
Now we will test for \(n\) = 1 to 9.
approxFactorial(9)
## [1] "n = 1"
## [1] "The approximation of n! is between: 0.995870161462797 and 1.00227444918223"
## [1] "The true value of n! is:1"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 2"
## [1] "The approximation of n! is between: 1.99732040475181 and 2.00065204769097"
## [1] "The true value of n! is:2"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 3"
## [1] "The approximation of n! is between: 5.99609587878364 and 6.00059914246899"
## [1] "The true value of n! is:6"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 4"
## [1] "The approximation of n! is between: 23.9908215434093 and 24.0010238913497"
## [1] "The true value of n! is:24"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 5"
## [1] "The approximation of n! is between: 119.969853959209 and 120.002637086197"
## [1] "The true value of n! is:120"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 6"
## [1] "The approximation of n! is between: 719.87221228743 and 720.009187306004"
## [1] "The true value of n! is:720"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 7"
## [1] "The approximation of n! is between: 5039.33474342902 and 5040.04058203609"
## [1] "The true value of n! is:5040"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 8"
## [1] "The approximation of n! is between: 40315.8880974138 and 40320.2177852255"
## [1] "The true value of n! is:40320"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"
## [1] "n = 9"
## [1] "The approximation of n! is between: 362850.55338988 and 362881.377885753"
## [1] "The true value of n! is:362880"
## [1] "n! is between the lower and upper bounds of the approximation: TRUE"
## [1] "---------------------"