Ctrl K

K-th Smallest Divisor

Medium
You are given positive integers number and index. Write out all divisors of number (the values d with number % d == 0) in ascending order and return the one at position index (counting from one). If there are fewer than index divisors, return -1. Trying every value up to number is the obvious way; try to get away with fewer steps.
Example 1
number=18, index=4 → 6

The divisors of 18 in ascending order are 1, 2, 3, 6, 9, 18. The fourth is 6.

Example 2
number=13, index=3 → -1

13 is prime and has only two divisors, 1 and 13, so there is no third.

Example 3
number=1, index=1 → 1
Constraints
  • 1 ≤ index ≤ number ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrimes Below a Bound MediumLaser in a Mirror Room MediumFirst Prime That Reads Both Ways MediumN-th Number Divisible by One of Three MediumReduced Fractions Up to a Denominator Medium
loading Python…
Input
number=18, index=4
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.