Skip to content

Instantly share code, notes, and snippets.

@thinklinux
Created August 22, 2015 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinklinux/5f45344d8e62d7a8bc4f to your computer and use it in GitHub Desktop.
Save thinklinux/5f45344d8e62d7a8bc4f to your computer and use it in GitHub Desktop.
Calculating prime numbers using Sieve of Eratosthenes
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
var sieve = [];
var primes = [];
var i, j;
var end = 1000;
for(i=2; i <= end; i++) {
if (!sieve[i]) {
primes.push(i);
for(j = i*2; j <= end; j += i) {
sieve[j] = true;
}
}
}
console.log(primes.join(', '));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment