Skip to content

Instantly share code, notes, and snippets.

@amarjitdhillon
Created February 11, 2022 01:23
Show Gist options
  • Save amarjitdhillon/ed318e17be3e09edf52b8e175593a692 to your computer and use it in GitHub Desktop.
Save amarjitdhillon/ed318e17be3e09edf52b8e175593a692 to your computer and use it in GitHub Desktop.
The kth Factor of n
class Solution:
def kthFactor(self, n: int, k: int) -> int:
# counter represent the i^th factor of the number found so far
counter = 0
# go over the number from 1 to n as we need to divide the n by this number to find if that number is a factor
for i in range(1, n+1):
if n%i == 0:
# increment the value of counter
counter += 1
# if the counter is equal to k, it means we have found the k^th factor
if k == counter:
return i
# if no factor is found then return -1
return -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment