Skip to content

Commit 61194de

Browse files
authored
Update 1492-the-kth-factor-of-n.js
1 parent c74ef85 commit 61194de

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
*/
66
const kthFactor = function (n, k) {
77
let d = 1
8-
for (; d * d <= n; ++d) {
9-
if (n % d == 0) {
10-
if (--k == 0) {
11-
return d
12-
}
8+
for (let i = 1; i * i < n; i++) {
9+
if (n % i === 0) {
10+
k--
11+
if (k === 0) return i
1312
}
1413
}
15-
for (d = d - 1; d >= 1; d--) {
16-
if (d * d == n) continue
17-
if (n % d == 0) {
14+
15+
for (let i = ~~Math.sqrt(n); i >= 1; i--) {
16+
if (n % ~~(n / i) === 0) {
1817
k--
19-
if (k == 0) {
20-
return n / d
21-
}
18+
// console.log(n, i, n/i, n % (n / i))
19+
if (k === 0) return n / i
2220
}
2321
}
22+
2423
return -1
2524
}

0 commit comments

Comments
 (0)