Multiply Every Integer From 1 Through N
Implement computeFactorial
Given a non-negative integer
n, compute the product of every integer from 1 up to n (with the standard convention that the product for n=0 is 1).
A single loop with a running product handles this directly, using no extra memory beyond the running total itself. The recursive framing expresses the same idea differently: the answer for n is just n multiplied by the answer for n-1, bottoming out once n reaches 1 or 0. Both do the same amount of multiplication — the difference is that recursion pays for it in stack frames: every call waits, paused, for the one below it to finish, so at the deepest point of the recursion there are n frames alive at once, where the loop only ever needed a single variable.
Example 1:
Input: n = 6
Output: 720
Example 2:
Input: n = 9
Output: 362880
Example 3:
Input: n = 12
Output: 479001600
+ 3 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ n ≤ 12 - ●
0! is defined as 1, matching the standard mathematical convention
n =
6