Skip to main content

Count Primes

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

Problems42 / 152
Sign in

Count Primes

MEDIUMArrayMath

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

Example 1

Input:  n = 10
Output: 4

Example 2

Input:  n = 0
Output: 0
Show hint

Use Sieve of Eratosthenes: mark multiples of each prime as non-prime. Start from 2, mark all multiples, then move to next unmarked number.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
n =
10
Expected
4
Run with your own input (JSON)