#1492
Medium Algorithms The kth factor of n
Math Number Theory
70.2% acceptance
Feb 25, 2026
1924
313
You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i == 0.
Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn kth_factor(n: i32, k: i32) -> i32 {
let mut count = 0i32;
for i in 1..=n {
if n % i == 0 {
count += 1;
if count == k { return i; }
}
}
-1
}
}