How to resolve the algorithm Factorial step by step in the R programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Factorial step by step in the R programming language
Table of Contents
Problem Statement
Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative n errors is optional.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Factorial step by step in the R programming language
Source code in the r programming language
fact <- function(n) {
if (n <= 1) 1
else n * Recall(n - 1)
}
factIter <- function(n) {
f = 1
if (n > 1) {
for (i in 2:n) f <- f * i
}
f
}
print(factorial(50)) # 3.041409e+64
You may also check:How to resolve the algorithm Function definition step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Zumkeller numbers step by step in the J programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Active object step by step in the FBSL programming language
You may also check:How to resolve the algorithm Check input device is a terminal step by step in the Crystal programming language