-
Notifications
You must be signed in to change notification settings - Fork 0
/
소수찾기.js
39 lines (29 loc) · 817 Bytes
/
소수찾기.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Solution 1
function solution1(n) {
const primeNumberArray = new Array(n).fill(1);
primeNumberArray[0] = 0;
for (let i = 2; i * i <= n; i++) {
for (let j = i * i; j <= n; j += i) {
primeNumberArray[j - 1] = 0;
}
}
const primeNumberCount = primeNumberArray.filter((flag) => flag === 1).length;
return primeNumberCount;
}
// Solution 2
function getPrimeNumberCount(num) {
const numbers = [false, false, ...Array(num - 1).fill(true)];
for (let i = 2; i * i <= num; i++) {
if (numbers[i]) {
for (let j = i * 2; j <= num; j += i) {
numbers[j] = false;
}
}
}
const primeNumberCount = numbers.filter(Boolean).length;
return primeNumberCount;
}
function solution2(n) {
const primeNumberCount = getPrimeNumberCount(n);
return primeNumberCount;
}