Вычислить факториал значения в программировании на R - функция factorial ()

Опубликовано: 18 Февраля, 2022

R Language offers a factorial() 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 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

Example 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