Skip to content

Instantly share code, notes, and snippets.

@methodin
Created December 28, 2011 19:41
Show Gist options
  • Save methodin/1529345 to your computer and use it in GitHub Desktop.
Save methodin/1529345 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
function primes(from, to) {
var arr = [];
for(var i=from;i<=to;i++) arr.push(i);
for(var check=0;check<to/2;check++) {
for(var j=check+arr[check]; j<=to,j<=arr.length-1; j+=arr[check]) {
arr.splice(j--,1);
}
}
return arr;
}
var list = primes(2,1000).join(', ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment