Given an integer n, return the number of prime numbers that are strictly less than n.
We use analytics and advertising cookies to understand how the site is used and whether our ads on Facebook and Instagram work. They are set only if you accept. See our Privacy Policy for details.
Given an integer n, return the number of prime numbers that are strictly less than n.
An easy math problem, graded against 8 test cases (5 of them hidden).
Number theory, digit manipulation, and closed-form reasoning.
Reach for it when you see: Primes, digits, base conversion, or a problem with a formula hiding behind it.
More Math problems →Create a boolean array of size n, initially all true. Starting from 2, for each prime number, mark all its multiples as not prime. Count the remaining true values.
Optimization: start marking multiples from i*i (smaller multiples are already marked).
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.