Math Fundamentals18 sections · 814 units
Open in Course

Count Primes - Algorithm

(Sieve then count)

1.1. Create a boolean array isPrime of size n, initialized to true.

2.2. Set isPrime[0] and isPrime[1] to false (0 and 1 are not prime).

3.3. For each number p from 2 to √n: if isPrime[p] is true, mark all multiples of p (starting from p²) as false.

4.4. Count how many entries in isPrime are true.