Skip to content

Instantly share code, notes, and snippets.

@chul-hyun
Last active October 21, 2016 02:21
Show Gist options
  • Save chul-hyun/a29ec13aa5053e61f7c97291a534d907 to your computer and use it in GitHub Desktop.
Save chul-hyun/a29ec13aa5053e61f7c97291a534d907 to your computer and use it in GitHub Desktop.
에라토스테네스의 체
function getPrime(max){
let sieve = _.fill(Array(max), true);
let _max = Math.floor(Math.sqrt(max));
sieve[0] = false;
sieve[1] = false;
for(let i = 2 ; i <= _max ; i++){
if(sieve[i]){
for(let j = i * 2 ; j <= max ; j += i){
sieve[j] = false;
}
}
}
let result = [];
sieve.forEach((check, index, arr)=>((check) && result.push(index)));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment