-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path012-highly-divisible-triangular-number.js
More file actions
61 lines (50 loc) · 1.21 KB
/
012-highly-divisible-triangular-number.js
File metadata and controls
61 lines (50 loc) · 1.21 KB
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
54
55
56
57
58
59
60
61
/**
* Highly Divisible Triangular Number
* Time Complexity: O(K √K)
* Space Complexity: O(1)
*/
function processData(input) {
input = input.trim().split(/\s+/).map(Number);
let t = input[0];
let idx = 1;
function countDivisors(n) {
let cnt = 0;
let root = Math.floor(Math.sqrt(n));
for (let i = 1; i <= root; i++) {
if (n % i === 0) {
cnt += 2;
}
}
if (root * root === n) cnt--;
return cnt;
}
while (t--) {
let N = input[idx++];
let k = 1;
while (true) {
let a, b;
if (k % 2 === 0) {
a = k / 2;
b = k + 1;
} else {
a = (k + 1) / 2;
b = k;
}
let divisors = countDivisors(a) * countDivisors(b);
if (divisors > N) {
console.log((k * (k + 1)) / 2);
break;
}
k++;
}
}
};
process.stdin.resume();
process.stdin.setEncoding("ascii");
let _input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});