Skip to content

Instantly share code, notes, and snippets.

@rosd89
Last active April 6, 2019 18:44
Show Gist options
  • Save rosd89/dacdddb341257f3f846b97b23254f0af to your computer and use it in GitHub Desktop.
Save rosd89/dacdddb341257f3f846b97b23254f0af to your computer and use it in GitHub Desktop.
const prime2 = end => {
if(end < 2) return [];
const prime = [2], num = [];
let curr = 2, i = 3, j;
while(i <= end){
num.push(i);
i += 2;
}
while(num.length){
prime.push(curr = num.shift());
i = num.length;
j = curr * curr;
while(i--){
if(num[i] < j) break;
if(num[i] % curr == 0) num.splice(i, 1);
}
if(j > num[num.length - 1]) break;
}
return prime.concat(num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment