Lv.1mathsieve
Count Primes up to N
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Return how many primes are in 1..n.
Examples
| n | result |
|---|---|
10 | 4 |
1 | 0 |
100 | 25 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(n: int) → int
Constraints
-
n
Expected complexity
- Time
- O(N log log N)
- Space
- O(N)
Examples
| n | Expected |
|---|---|
10 | 4 |
1 | 0 |
100 | 25 |
Hint
Hint
Use the sieve of Eratosthenes: for every surviving p with , cross out . Use a byte array.
Trial division up to for every number is and exceeds the time limit.
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) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 9 | verdict only |
| performance | 60 | 4 | verdict only |