1. A more refined inequality for approximating n! is given by

\[\sqrt{2\pi n}(\frac{n}{2})^ne^{1/(12n+1)} < n! < \sqrt{2\pi n}(\frac{n}{2})^ne^{1/(12n)}\]

Write a computer program to illustrate this inequality for n = 1 to 9.

n_factoral <- function(n) {
  # Perform a check for n = 1 to 9.
  if (1 <= n & n <= 9) {
    n_check = TRUE
  } else return("n must be equal to 1 to 9")
  
  if (n_check == TRUE) {
    A = sqrt(2*pi*n)*((n/exp(1))^n)*exp((1/(12*n+1)))
    B = factorial(n)
    C = sqrt(2*pi*n)*((n/exp(1))^n)*exp((1/(12*n)))
    
    
    if (A < B & B < C) {
      n_approx = TRUE
      return(n_approx)
      
      
    }else return(FALSE)
    
  }
} 


A_B_C <- function(n) {
  # Print out the 3 values of input n
  A = sqrt(2*pi*n)*((n/exp(1))^n)*exp((1/(12*n+1)))
  B = factorial(n)
  C = sqrt(2*pi*n)*((n/exp(1))^n)*exp((1/(12*n)))
  
  my_list = list(A, B, C)
  
  return(my_list)
} 
# Test with various values of n
n = 0
n_factoral(n)
## [1] "n must be equal to 1 to 9"
# A visual on the values of A, B and C
A_B_C(n)
## [[1]]
## [1] 0
## 
## [[2]]
## [1] 1
## 
## [[3]]
## [1] NaN
# Test with various values of n
n = 1
n_factoral(n)
## [1] TRUE
# A visual on the values of A, B and C
A_B_C(n)
## [[1]]
## [1] 0.9958702
## 
## [[2]]
## [1] 1
## 
## [[3]]
## [1] 1.002274
# Test with various values of n
n = 9
n_factoral(n)
## [1] TRUE
# A visual on the values of A, B and C
A_B_C(n)
## [[1]]
## [1] 362850.6
## 
## [[2]]
## [1] 362880
## 
## [[3]]
## [1] 362881.4
# Test with various values of n
n = 10
n_factoral(n)
## [1] "n must be equal to 1 to 9"
# A visual on the values of A, B and C
A_B_C(n)
## [[1]]
## [1] 3628560
## 
## [[2]]
## [1] 3628800
## 
## [[3]]
## [1] 3628810

Note: The test for any values for n >= 10 is also true, but we are restricting our test to n = 1 to 9.