Calculate Factorial
Computes the factorial of a given integer.
- math
- factorial
Code
def factorial(n)
return 1 if n <= 1
(2..n).reduce(1, :*)
endUsage
puts factorial(5) # Output: 120Computes the factorial of a given integer.
def factorial(n)
return 1 if n <= 1
(2..n).reduce(1, :*)
endputs factorial(5) # Output: 120