There is a built in factorial()
function in R that calculates the factorial of integers.
For example:
paste("The factorial of 5 is:", factorial(5))
## [1] "The factorial of 5 is: 120"
For integer N (N > 0), the factorial of N can be written as:
N! = N * (N - 1) * ( N - 2) * … * 3 * 2 * 1
Moreover, 0! is defined to be 1:
0! = 1
This challenge is to write a function that uses a single loop to compute N.
For example:
4! = 4 * (4-1) * (4-2) * (4-3), for N equals to 4.
##########
# Function
##########
fact_looping <- function(N) {
val <- 1 # to temporarily store results
if (N == 0 | N == 1) {
return(1) # for 0! = 1 and 1! = 1
} else {
for (i in 2:N) {
val <- val * i
}
return(val)
}
}
##########
# Test
##########
N <- 10
paste("For N = 10, the result of my looping function is:", fact_looping(N))
## [1] "For N = 10, the result of my looping function is: 3628800"
paste("10! equals to:", factorial(N))
## [1] "10! equals to: 3628800"
isFunctional <- fact_looping(N) == factorial(N)
paste("Are they the same?", isFunctional, "!")
## [1] "Are they the same? TRUE !"
N <- 6
paste("For N = 6, the result of my looping function is:", fact_looping(N))
## [1] "For N = 6, the result of my looping function is: 720"
paste("6! equals to:", factorial(N))
## [1] "6! equals to: 720"
isFunctional <- fact_looping(N) == factorial(N)
paste("Are they the same?", isFunctional, "!")
## [1] "Are they the same? TRUE !"
N <- 1
paste("For N = 1, the result of my looping function is:", fact_looping(N))
## [1] "For N = 1, the result of my looping function is: 1"
paste("1! equals to:", factorial(N))
## [1] "1! equals to: 1"
isFunctional <- fact_looping(N) == factorial(N)
paste("Are they the same?", isFunctional, "!")
## [1] "Are they the same? TRUE !"
Now, write that same function for N! without a loop using recursion
.
Hint
Think about the definition of factorial:
N! = N*((N - 1)!)
unless N = 0
Hint
You might use a conditional expression to handle N <= 1.
##########
# Function
##########
fact_recursive <- function(N) {
if (N <= 1) {
return(1) # for 0! = 1 and 1! = 1
} else {
return(N * fact_recursive(N - 1))
}
}
##########
# Test
##########
N <- 10
paste("For N = 10, the result of my recursive function is:", fact_recursive(N))
## [1] "For N = 10, the result of my recursive function is: 3628800"
paste("10! equals to:", factorial(N))
## [1] "10! equals to: 3628800"
isFunctional <- fact_recursive(N) == factorial(N)
paste("Are they the same?", isFunctional, "!")
## [1] "Are they the same? TRUE !"
N <- 6
paste("For N = 6, the result of my recursive function is:", fact_recursive(N))
## [1] "For N = 6, the result of my recursive function is: 720"
paste("6! equals to:", factorial(N))
## [1] "6! equals to: 720"
isFunctional <- fact_recursive(N) == factorial(N)
paste("Are they the same?", isFunctional, "!")
## [1] "Are they the same? TRUE !"
N <- 1
paste("For N = 1, the result of my recursive function is:", fact_recursive(N))
## [1] "For N = 1, the result of my recursive function is: 1"
paste("1! equals to:", factorial(N))
## [1] "1! equals to: 1"
isFunctional <- fact_recursive(N) == factorial(N)
paste("Are they the same?", isFunctional, "!")
## [1] "Are they the same? TRUE !"