Skip to content
BytePatterns

Primes Below A Limit

MediumMath & Number Theory#sieve#precomputation~25m

Problem

Count how many prime numbers are strictly smaller than a given limit. The limit can reach a few million, so testing each candidate against its divisors is far too slow.

Examples

Input:  limit = 10
Output: 4
Why:    2, 3, 5 and 7
Input:  limit = 100
Output: 25
Input:  limit = 2
Output: 0
Why:    edge case, nothing below 2 is prime

Hints

0 / 3

Stuck on the idea rather than the code? Sieve of Eratosthenes covers it.