Count Primes

Given an integer n, return the number of prime numbers that are strictly less than n.

Example 1
Inputn = 10
Output4
There are 4 prime numbers less than 10: 2, 3, 5, and 7.
Example 2
Inputn = 0
Output0
There are no prime numbers strictly less than 0.

Constraints

  • 0 <= n <= 5 * 10^6

Asked at 17 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate