factorial.sh — zsh
mathrecursion

Return n!, the product of all positive integers from 1 through n. By definition, 0! = 1.

Input / output

  • Input: n: integer
  • Output: integer factorial

Examples

  1. n = 0 returns 1.
  2. n = 5 returns 120.

Constraints

  • 0 <= n <= 12 so the result fits a 32-bit signed integer

Follow-up What overflow and recursion-depth issues appear when the input range grows?

Examples
Example 1
Input: n = 0
Output: 1
Example 2
Input: n = 5
Output: 120
Example 3
Input: n = 7
Output: 5040
🔒 5 hidden
Running will execute all 8 cases, including 5 hidden ones.