We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aa13f37 commit c74ef85Copy full SHA for c74ef85
1492-the-kth-factor-of-n.js
@@ -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
18
+ k--
19
+ if (k == 0) {
20
+ return n / d
21
22
23
24
+ return -1
25
+}
0 commit comments