How to Find the Factorial of a Number
Last Updated :
15 Sep, 2023
In mathematics, we often encounter the concept of factorial denoted as n! which represents the product of all integers less, than, or equal to a given non-negative integer n. Factorials find applications in combinatorics, probability, and other mathematical fields. In the R programming language, you have options to calculate factorials using either built-in functions or your own custom code.
Here are some key concepts related to factorials
1. Factorial refers to the product of all integers from 1 up to a number.
2. Recursive Approach: This involves solving problems by breaking them into subproblems that follow a similar pattern.
3. Iterative Approach: Alternatively we can compute factorials using loops and repetitive structures.
To calculate the factorial of a number, in R you can follow these steps:
a) Utilizing the built-in function ():
1. Choose the number for which you wish to find its value.
2. Apply the factorial() function to compute the desired result.
3. Print out the obtained value.
R
num <- 5
factorial_result <- factorial (num)
print ( paste ( "Factorial of" , num, "is" , factorial_result))
|
Output:
Factorial of 5 is 120
b) Creating a custom function ( iterative):
1. Define your function that accepts an input number as an argument.
2. Check if the input is either 0 or 1; if so return 1 (base case).If the number provided is, than 1 you can. Use iteration to calculate the factorial. Once you have computed the value make sure to return it.
R
iterative_factorial <- function (n) {
result <- 1
for (i in 1:n) {
result <- result * i
}
return (result)
}
num <- 7
factorial_result_iterative <- iterative_factorial (num)
print ( paste ("Factorial of", num, "using iterative approach is",
factorial_result_iterative))
|
Output:
Factorial of 7 using iterative approach is 5040
- We assigns the value 7 to the variable
num
.
factorial_result_iterative <- iterative_factorial(num)
:
- This calculates the factorial of the number stored in
num
using the iterative_factorial
function.
- The result is stored in the variable
factorial_result_iterative
.
print(paste("Factorial of", num, "using iterative approach is", factorial_result_iterative))
:
- This line constructs a message that includes the original number, the fact that the calculation is done using the iterative approach, and the calculated factorial value.
- The
paste()
function combines these components into a single message.
c)Creating a custom function ( Recursive):
1. Define your function that accepts an input number as an argument.
2. Check if the input is either 0 or 1; if so return 1 (base case).If the number provided is, than 1 you can. Use recursion to calculate the factorial. Once you have computed the value make sure to return it.
R
recursive_factorial <- function (n) {
if (n == 0 || n == 1) {
return (1)
} else {
return (n * recursive_factorial (n - 1))
}
}
num <- 6
factorial_result_recursive <- recursive_factorial (num)
print ( paste ( "Factorial of" , num, "using recursive approach is" ,
factorial_result_recursive))
|
Output:
Factorial of 6 using recursive approach is 720
- First assigns the value 6 to the variable
num
.
factorial_result_recursive <- recursive_factorial(num)
:
- This calculates the factorial of the number stored in
num
using the recursive_factorial
function.
- The result is stored in the variable
factorial_result_recursive
.
print(paste("Factorial of", num, "using recursive approach is", factorial_result_recursive))
:
- This line constructs a message that includes the original number, the fact that the calculation is done using the recursive approach, and the calculated factorial value.
- The
paste()
function combines these components into a single message.
- The
print()
function then outputs the message to the console.
Share your thoughts in the comments
Please Login to comment...