PythonEasyNumbers10 min
Compute the factorial of a number
Use the convention 0! = 1. Python integers can represent the exact result without fixed-width overflow. Interviewers use this problem to assess accumulate a product starting at the multiplicative identity.
Input
One integer n.
Output
Print the result on one line; print an empty line for an empty string.
Constraints
0 <= n <= 500.
Example
Input
5
Output
120
Example 2 input
50
Output
30414093201713378043612608166064768844377641568960512000000000000
The larger input exercises arbitrary-precision integers.
Public test cases (2)
Input
5
Expected output
120
Input
10
Expected output
3628800
Hints (2)
- Identify the state needed to avoid repeated work.
- Accumulate a product starting at the multiplicative identity.
Target complexity: time O(n) arithmetic operations · space O(n log n) bits for the result
