Count Primes
Medium Math & Geometry
A prime is a whole number greater than 1 that no whole number divides except 1 and itself: 2, 3, 5, 7, 11 and so on.
Given n, count the primes that are less than n. n itself is not counted, even when it is a prime.
Return that count, which is 0 when n is 2 or less.
Example 1
Input: n = 20
Output: 8
The primes less than 20 are 2, 3, 5, 7, 11, 13, 17 and 19: eight of them.
Example 2
Input: n = 3
Output: 1
Example 3
Input: n = 100
Output: 25
Constraints
0 ≤ n ≤ 5 × 10⁶
Solve it here
Write it in Python, Java or C++, run it on your own cases, and submit against hidden tests.
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
See the AI roundMore Math & Geometry
- Rotate ImageAI round · Medium
- Roman to IntegerAI round · Easy
- Palindrome NumberAI round · Easy
- Reverse IntegerMedium
- Integer to RomanMedium