Last Updated: September 12, 2021
·
187
· Bruno Volpato

Prime Number Generator

Prime Number Generator from 2..N

boolean[] primes = new boolean[n+1];
for (int i = 0; i <= n; i++) {
    primes[i] = true;
}

// Sieve of Eratosthenes
for (int p = 2; p*p <= n; p++) {

    if (primes[p]) {
        for (int i = p * p; i <= n; i += p) {
            primes[i] = false;
        }
    }
}