-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
38 lines (38 loc) · 1 KB
/
solution.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
const filePath = require('path').join(__dirname, 'input');
const [...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n')
.map(Number);
arr.pop();
function solution (arr) {
const max = Math.max.apply(null, arr);
const eratos = n => {
const primes = new Array(n + 1).fill(1);
for (let i = 2; (i * i) <= n; i ++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = 0;
}
}
}
return primes;
}
const primes = eratos(max);
const answer = [];
for (let i = 0; i < arr.length; i ++) {
const n = arr[i];
for (let j = 3; j < n; j ++) {
if (primes[j]) {
const k = arr[i] - j;
if (primes[k]) {
answer.push(`${arr[i]} = ${j} + ${k}`);
break;
}
}
}
}
return answer.join('\n');
}
console.log(solution(arr));