How to use Factorial() Function In R Language
R Programming Language offers a factorial()
a function that can compute the factorial of a number without writing the whole code for computing factorial.
Syntax: factorial(x)
Parameters:x: The number whose factorial has to be computed.
Returns: The factorial of the desired number.
Example 1
R
# R program to calculate factorial value # Using factorial() method answer1 <- factorial (4) answer2 <- factorial (-3) answer3 <- factorial (0) print (answer1) print (answer2) print (answer3) |
Output:
24
NaN
1
Example 2
Compute the factorial of multiple values
R
# R program to calculate factorial value # Using factorial() method answer1 <- factorial ( c (0, 1, 2, 3, 4)) print (answer1) |
Output:
1 1 2 6 24
Calculate Factorial of a value in R Programming – factorial() Function
The factorial of a non-negative integer n
, denoted by n!
, is the product of all positive integers less than or equal to n
.
It is defined as:
n! = n × (n - 1) × (n - 2) × ... × 2 × 1
For example
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 0! is defined to be 1.
Factorials have important applications in mathematics, combinatorics, and probability theory.