Skip to content

Instantly share code, notes, and snippets.

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