Sieve of Eratosthenes

PHOTO EMBED

Fri Apr 12 2024 09:05:38 GMT+0000 (Coordinated Universal Time)

Saved by @devdutt

int n;
vector<bool> is_prime(n+1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
    if (is_prime[i] && (long long)i * i <= n) {
        for (int j = i * i; j <= n; j += i)
            is_prime[j] = false;
    }
}
content_copyCOPY

https://cp-algorithms.com/algebra/sieve-of-eratosthenes.html