Count primes below n
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Count primes below n
Return how many prime numbers are strictly less than n. A prime has no divisors other than 1 and itself. For each candidate k, test divisors only up to √k (i.e. while d * d <= k) — once one divides k evenly, it isn't prime.
Complete countPrimes(int n) returning how many prime numbers are strictly less than n. Example: countPrimes(10) is 4 (2, 3, 5, 7).
Click Run to see the output here.