InferenceAI

Enter at least two characters to search.

Start free

Count Primes Below N

mediumMath25 min25 XP

Return how many prime numbers are strictly less than n.

Requirements

  • Use the Sieve of Eratosthenes: O(nlog⁡log⁡n)O(n \log \log n) time.

Constraints

  • 0≤n≤5⋅1060 \le n \le 5 \cdot 10^6

Examples

EXAMPLE WALKTHROUGHprimes 2, 3, 5, 7

A guided view of the published example, not a trace of your code.

INPUT[10]
TASKcount_primes_below()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

Input: n = 10

Output: 4

Explanation: primes 2, 3, 5, 7

Input: n = 0

Output: 0

Explanation: zero

Input: n = 2

Output: 0

Explanation: two is not below two

Hint 1

Mark multiples of each prime p as composite, starting from p * p.

Hint 2

You only need to sieve with p up to the square root of n.

Topics

DSAMathPython