Skip to content

Commit c74ef85

Browse files
authored
Create 1492-the-kth-factor-of-n.js
1 parent aa13f37 commit c74ef85

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

1492-the-kth-factor-of-n.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const kthFactor = function (n, k) {
7+
let d = 1
8+
for (; d * d <= n; ++d) {
9+
if (n % d == 0) {
10+
if (--k == 0) {
11+
return d
12+
}
13+
}
14+
}
15+
for (d = d - 1; d >= 1; d--) {
16+
if (d * d == n) continue
17+
if (n % d == 0) {
18+
k--
19+
if (k == 0) {
20+
return n / d
21+
}
22+
}
23+
}
24+
return -1
25+
}

0 commit comments

Comments
 (0)