QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Calculate Factorial

Computes the factorial of a given integer.

  • math
  • factorial

Code

def factorial(n)
    return 1 if n <= 1
    (2..n).reduce(1, :*)
end

Usage

puts factorial(5) # Output: 120