-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb1644.js
More file actions
53 lines (45 loc) · 985 Bytes
/
b1644.js
File metadata and controls
53 lines (45 loc) · 985 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
46
47
48
49
50
51
52
53
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim();
const num = +input;
console.log(getConsecutiveSumCnt(getPrimes(num), num));
function getConsecutiveSumCnt(nums, target) {
let sum = 0;
let left = -1,
right = -1;
let cnt = 0;
while (left <= right && right < nums.length - 1) {
if (sum < target) {
right += 1;
sum += nums[right];
} else if (sum > target) {
left += 1;
sum -= nums[left];
} else {
cnt += 1;
right += 1;
sum += nums[right];
}
}
while (left <= right && sum > target) {
left += 1;
sum -= nums[left];
}
if (sum === target) {
cnt += 1;
}
return cnt;
}
function getPrimes(n) {
const nums = Array(n + 1)
.fill(0)
.map((_, i) => i);
nums[1] = 0;
for (let i = 2; i <= n; i++) {
if (nums[i] !== 0) {
for (let j = i + i; j <= n; j += i) {
nums[j] = 0;
}
}
}
return nums.filter((num) => num !== 0);
}