-
Notifications
You must be signed in to change notification settings - Fork 0
/
050.js
48 lines (41 loc) · 976 Bytes
/
050.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
48
const { isPrime } = require("./utils");
console.time("50");
const max = 1000000;
const primes = [];
for (let i = 1; i < max; i++) {
if (isPrime(i)) {
primes.push(i);
}
}
const solutions = [];
for (let i = 0; i <= primes.length; i++) {
let solution = { sum: 0, count: 0 };
let keepGoing = true;
for (let j = i; j < primes.length && keepGoing; j++) {
solution = { sum: solution.sum + primes[j], count: solution.count + 1 };
if (isPrime(solution.sum) && solution.sum <= max) {
solutions.push(solution);
} else if (solution.sum > max) {
keepGoing = false;
}
}
}
console.log(
solutions.reduce(
(acc, curr) => {
if (curr.count > acc.count) {
return curr;
} else if (curr.count === acc.count) {
if (curr.sum > acc.sum) {
return curr;
} else {
return acc;
}
} else {
return acc;
}
},
{ sum: 0, count: 0 }
)
);
console.timeEnd("50");