在 R 编程中计算值的阶乘 - factorial()函数
R 语言提供了一个factorial()
函数,它可以计算一个数的阶乘,而无需编写整个代码来计算阶乘。
Syntax: factorial(x)
Parameters:
x: The number whose factorial has to be computed.
Returns: The factorial of desired number.
示例 1:
# R program to calculate factorial value
# Using factorial() method
answer1 <- factorial(4)
answer2 <- factorial(-3)
answer3 <- factorial(0)
print(answer1)
print(answer2)
print(answer3)
输出:
24
NaN
1
示例 2:
# R program to calculate factorial value
# Using factorial() method
answer1 <- factorial(c(0, 1, 2, 3, 4))
print(answer1)
输出:
1 1 2 6 24