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;
}
}
}
Written by Bruno Candido Volpato da Cunha
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Java
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#