Skip to content
RED SAGSkills
medium30 ptsmath

Count Primes Below N

Return how many prime numbers are strictly less than n.

Example 1

Input: n = 10
Output: 4
Explanation: The primes below 10 are 2, 3, 5 and 7.

Example 2

Input: n = 2
Output: 0

Constraints

  • 0 <= n <= 2 * 10^6
Hint 1

Use the Sieve of Eratosthenes.

Hint 2

Start crossing out multiples of p at p * p.

Discussion 0

Join free to post in the discussion.

Good practice for: Embedded Systems Engineer, Game Developer, Backend Developer, Blockchain Developer, Data Scientist

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.