-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb6588.js
More file actions
45 lines (37 loc) · 885 Bytes
/
b6588.js
File metadata and controls
45 lines (37 loc) · 885 Bytes
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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const cases = input.map(Number);
cases.pop();
const N = 1000000;
function getPrimes(n) {
const nums = Array(n + 1)
.fill(0)
.map((_, i) => i);
for (let i = 2; i <= n; i++) {
if (nums[i] !== 0) {
for (let j = i + i; j <= n; j += i) {
nums[j] = 0;
}
}
}
nums[0] = 0;
nums[1] = 0;
return nums;
}
const primes = getPrimes(N);
const onlyPrimes = primes.filter((n) => n !== 0);
let res = "";
cases.forEach((n) => {
for (let i = 0; i < onlyPrimes.length; i++) {
const gap = n - onlyPrimes[i];
if (primes[gap]) {
res += `${n} = ${onlyPrimes[i]} + ${gap}\n`;
break;
} else {
if (i === onlyPrimes.length - 1) {
res += `Goldbach's conjecture is wrong.\n`;
}
}
}
});
console.log(res.trim());