easy
math
recursion
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
n = 0returns1.n = 5returns120.
Constraints
0 <= n <= 12so 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.