const fs = require('fs');
const path = process.platform === 'linux' ? '/dev/stdin' : './data.txt';
const input = fs
.readFileSync(path)
.toString()
.split('\n')
.map((x) => Number(x));
function solve(input) {
for (let number of input) {
if (number === 0) {
return null;
}
let min = number;
let max = number * 2;
let arr = [...new Array(max + 1).keys()];
for (let i = 2; i <= Math.sqrt(max); i++) {
if (arr[i]) {
for (let j = i * i; j <= max; j += i) {
arr[j] = false;
}
}
}
arr.shift();
arr.shift();
console.log(arr.filter((x) => x > min).length);
}
}
solve(input);
์๋ผํ ์ค๋คํ
์ค์ ์ฒด ๊ฐ๋
๋ง ์ดํดํ๊ณ ๋ก์ง์ ์งค ์ค ์๋ฉด ์ฝ๊ฒ ํ๋ฆฌ๋ ๋ฌธ์ ๊ฐ๋ค.
๊ฐ์ธ์ ์ผ๋ก ๋ฐฐ์ด์ ๋ง์ด ๋ค๋ฃจ๋ฉด์ ๋ฐฐ์ด์ ํจ์๋ค์ MDN์์ ์ฐพ์๋ณด๋ฉด์ ํ์ฉํ๋ ๊ฒ ๋ฌด์ฒ ์ฌ๋ฏธ์๋ค. :)
'๐ JavaScript > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS] 10872 (0) | 2022.05.28 |
---|---|
[JS] 9020 (0) | 2022.05.23 |
[JS] 1929 (0) | 2022.05.22 |
[JS] 11653 (0) | 2022.05.22 |
[JS] 2581 (0) | 2022.05.22 |