-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.js
47 lines (40 loc) · 911 Bytes
/
answer.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
40
41
42
43
44
45
46
47
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let arr = [];
const solve = async (arr) => {
for (let N of arr) {
if(N === 0) break;
if(N === 1) {
console.log(1);
continue;
}
const start = N+1;
const last = N*2;
let cnt = 0;
const arr = new Array(last + 1);
arr.fill(true);
for(let i=2; i <= Math.sqrt(arr.length); i++) {
if(arr[i] === false) continue;
for(let j = i*i; j <= arr.length; j += i) {
if(arr[j]) arr[j] = false;
}
}
for(let i=start; i<last; i++) if(arr[i] === true) cnt++;
console.log(cnt);
}
return;
}
rl.on("line", function(line) {
const N = Number(line);
arr.push(N);
if(Number(line) === 0) {
solve(arr).then((result) => {
rl.close();
})
}
}).on("close", function() {
process.exit();
});