Factorial
EasyP2Coding
Return n!, the product of all integers from 1 to n. By definition 0! = 1.
Solve it recursively: n! = n * (n - 1)!, with a base case that stops the recursion.
Example 1
- Input
- n = 5
- Output
- 120
- Why
- 5 * 4 * 3 * 2 * 1 = 120.
Example 2
- Input
- n = 0
- Output
- 1
Example 3
- Input
- n = 1
- Output
- 1
Constraints
- 0 <= n <= 18 (so the result fits in a 53-bit integer)
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.