Skip to content

Instantly share code, notes, and snippets.

@sota1235
Created July 14, 2014 19:21
Show Gist options
  • Save sota1235/c010c337767bc1e9325d to your computer and use it in GitHub Desktop.
Save sota1235/c010c337767bc1e9325d to your computer and use it in GitHub Desktop.
function sieve(n) {
// make flag list
var num = [false, false];
for(i=0;i<n-2;i++) {
num.push(true);
}
for(i=2;i<=Math.sqrt(n);i++) {
if(num[i]) {
for(j=i*i;j<n;j+=i){
num[j] = false;
}
}
}
return num;
}
list = sieve(999999);
ans = [];
for(i=2;i<list.length;i++){
if(list[i]) {
ans.push(i);
}
}
console.log(ans);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment