EasyFoundational
Factorial of a Number
- Time limit
- 2 s
- Memory limit
- 128 MB
- Estimated
- 10 min
- Recursion Fundamentals
Description
Write a recursive function that computes the factorial of a non-negative integer n, defined as:
factorial(0) = 1factorial(n) = n * factorial(n - 1)forn > 0
Because factorials grow very quickly, return the result as a decimal string (e.g. "120"), not a number, so arbitrarily large results can be represented exactly.
Example: n = 5 returns "120", because 5 * 4 * 3 * 2 * 1 = 120.
You must use recursion (no iterative loops building up the product) to receive full credit from reviewers, although any correct implementation will pass the automated tests.
Constraints
0 <= n <= 100
Sample cases
3Sample 1
Input
n = 0
Expected output
"1"
Sample 2
Input
n = 1
Expected output
"1"
Sample 3
Skills and languages
Concepts
- Recursion
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer