Sieve of Eratosthenes
Find all the prime numbers <= n.
vector<int> isPrime(n+1, 1);
for (int i=2; i*i<=n; i++) {
if (!isPrime[i])
continue;
for (int j=i*i; j<=n; j+=i) {
isPrime[j] = 0;
}
}
Last updated
Find all the prime numbers <= n.
vector<int> isPrime(n+1, 1);
for (int i=2; i*i<=n; i++) {
if (!isPrime[i])
continue;
for (int j=i*i; j<=n; j+=i) {
isPrime[j] = 0;
}
}
Last updated