Skip to content

Instantly share code, notes, and snippets.

@zlrlo
Last active October 3, 2023 02:26
Show Gist options
  • Select an option

  • Save zlrlo/1155f23cbddd10ff4d834443a730f0e4 to your computer and use it in GitHub Desktop.

Select an option

Save zlrlo/1155f23cbddd10ff4d834443a730f0e4 to your computer and use it in GitHub Desktop.
[JS] 소수 구하는 가장 빠른 방법
function getPrime(n) {
let answer = 0;
let prime = [false, false, ...Array(n - 1).fill(true)];
for(let i = 2; i * i <= n; i++) {
if(prime[i]) {
for(let j = i * 2; j <= n; j += i) {
prime[j] = false;
}
}
}
answer = prime.filter(Boolean).length;
return answer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment