Updated March 21, 2023
Introduction to Factorial in R
A mathematical concept which is based on the idea of calculation of product of a number from one to the specified number, with multiplication working in reverse order i.e. starting from the number to one, and is common in permutations and combinations and probability theory, which can be implemented very effectively through R programming either through user-defined functions or by making use of an in-built function, is known as factorial in R programming.
Let us see some examples to find factorial –
The factorial of 0 = 0! = 1.
The factorial of 1 = 1! = 1.
The factorial of 2 = 2! = n* ( n – 1)= 2* ( 2 – 1) = 2 * 1 = 2.
The factorial of 3 = 3! = n* ( n – 1) * (n – 2)= 3* ( 3 – 1)* (3 – 2) = 3 * 2 * 1 = 6.
The factorial of 4 = 4! = n* ( n – 1) * (n – 2)* (n – 3)= 4* ( 4 – 1)* (4 – 2)* (4 – 3) =4* 3 * 2 * 1 = 24.
The factorial of 5 = 5! = n* ( n – 1) * (n – 2)* (n – 3)* (n – 4) = 5* ( 5 – 1)* (5 – 2)* (5 – 3)* (5 – 4) =5 * 4 * 3 * 2 * 1 = 120. And so on.
As in the above calculation, we have seen that the factorial of 0 is 1, whereas the factorial of the negative number is not defined, in R we get NAN as the output for factorial of the negative number.
How to Find Factorial in R Programming?
Here we will discuss the program to calculate the factorial using various methods.
Example #1 – Factorial using if-else statement
Code:
facto <- function(){
# accept the input provided by the user and convert to integer
no = as.integer( readline(" Input a number to find factorial : "))
fact = 1
# checking whether the number is negative, zero or positive
if(no < 0) {
print(" The number is negative the factorial does not exist. ")
} else if(no == 0) {
print(" The factorial result is 1 ")
} else {
for( i in 1:no) {
fact = fact * i
}
print(paste(" The factorial result is ", no ,"is", fact ))
}
}
facto()
Output:
The output of the above code for negative number–
In the above code the if-else statement first check whether the no is negative or not, if the no is negative means no < 0 condition is true then output display “ The number is negative the factorial does not exist ”, Whereas if condition is false then the else if no == 0 condition checks, if its true the output display “The factorial is 1”, else with the for loop calculate the factorial and display calculated value as output.
Example #2 – Factorial using for loop
Code:
facto <- function(){
no = as.integer( readline(prompt=" Enter a number to find factorial : "))
fact = 1
for( i in 1:no) {
fact = fact * i
}
print(paste(" The factorial of ", no ,"is", fact ))
}
facto()
Output:
In the above code, it is just finding the factorial without checking whether the number is negative or not.
Example #3 – Factorial using recursion Method
Code:
fact <- function( no ) {
# check if no negative, zero or one then return 1
if( no <= 1) {
return(1)
} else {
return(no * fact(no-1))
}
}
Output:
The output of the above code for negative number–
The output of the above code for zero number–
The output of the above code for positive number–
The above code using the recursive function fact( ), inside the fact( ) function the factorial is finding by product of the each number recursively by the line return(no * fact(no-1)). Suppose we call fact function as fact(7) then the function fact() recursively as given below –
no = 7
if (no <= 1) -> false
return no * fact(no-1) => 7 * fact(6) => 7 * 6 * fact(5) => => 7 * 6 * 5 * fact(4) => 7 * 6 * 5 * 4 * fact(3) => 7 * 6 * 5 * 4 * 3 * fact(2) => 7 * 6 * 5 * 4 * 3 * 2 * fact(1) => 7 * 6 * 5 * 4 * 3 * 2 * 1 => 5040. So the final result is 5040.
Example #4 – Factorial using the built-in function
The factorial( ) function is the built-in function of R language which is used to calculate the factorial of a number. The syntax of the function is –
factorial( no )
no – numeric vector
Some of the example for factorial( no ) function with different parameters –
# find the factorial of -1
> factorial(-1)
[1] NaN
# find the factorial of 0
> factorial(0)
[1] 1
# find the factorial of 1
> factorial(1)
[1] 1
# find the factorial of 7
> factorial(7)
[1] 5040
# find the factorial for vector of each elements 2, 3, 4
> factorial(c(2,3,4))
[1] 2 6 24
Conclusion
- The product of all the numbers from 1 to the specified number is called the factorial of a specified number.
- The formula or logic used to find the factorial of n number is n! = n* ( n – 1)* ( n – 2)* ( n – 3)….
- The factorial of 0 is 1, the factorial of all negative number is not defined in R it outputs NAN.
- In R language the factorial of a number can be found in two ways one is using them for loop and another way is using recursion (call the function recursively).
Recommended Articles
This is a guide to Factorial in R. Here we discuss introduction of Factorial in R along with examples to calculate factorial using variety of methods. You can also go through our other suggested articles to learn more –