Tutorialmath
Factorial
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer n, return its factorial , with .
The value grows fast, so the return type is a 64-bit integer (long); is about .
Examples
| n | result |
|---|---|
5 | 120 |
0 | 1 |
1 | 1 |
Example 1 — 1 × 2 × 3 × 4 × 5 = 120.
Example 2 — 0! is defined as 1.
Example 3 — 1! = 1.
This is a tutorial problem: there are no performance tests, just return the correct value.
Function signature
solution(n: int) → long
Constraints
-
n
Expected complexity
- Time
- O(N)
- Space
- O(1)
Examples
| n | Expected |
|---|---|
5 | 120 |
0 | 1 |
1 | 1 |
Hint
Hint
Start a result variable at 1 and multiply by 2, 3, …, n in a loop. In C, C++, Java and C#, declare it as
long (long long in C/C++): an int overflows from on.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1000 ms | derived |
| Kotlin 2.4 (JVM) | 1200 ms | estimated |
| C# 14 (.NET 10) | 1200 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 100 | 10 | verdict only |