Skip to content

Instantly share code, notes, and snippets.

@tigerclaw-az
Created December 16, 2013 22:45
Show Gist options
  • Save tigerclaw-az/7995873 to your computer and use it in GitHub Desktop.
Save tigerclaw-az/7995873 to your computer and use it in GitHub Desktop.
function prime(num) {
var primes = [],
i, j;
if (num < 2) return [];
for (i = 2; i <= num; i++) { primes[i] = true; }
for (i = 2; i <= Math.floor(Math.sqrt(num)); i++) {
var n = 0;
if (primes[i]) {
for (j = Math.pow(i, 2); j <= num; j = Math.pow(i,2)+n*i) { primes[j] = false; n++ }
}
}
primes = primes.map(function(p, i) {
if (p)return i;
}).filter(function(p) { return p; });
return primes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment