Last active
April 6, 2019 18:44
-
-
Save rosd89/dacdddb341257f3f846b97b23254f0af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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