Factorial
Problem statement
Return n!, the product of all integers from 1 through n. By definition, 0! = 1.
First implement the recursive recurrence, then compare it with an iterative implementation that uses constant extra space. The judge checks the returned value.
Function
factorial(n: int) → intExamples
Example 1
n = 5return = 120The product 1 × 2 × 3 × 4 × 5 is 120.
Example 2
n = 0return = 1The empty product is defined as 1.
Constraints
0 <= n <= 12.