serega1112

1492

Jan 13th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. class Solution:
  2.     def kthFactor(self, n: int, k: int) -> int:
  3.        
  4.         divisors = []
  5.         counterparts = []
  6.         res = -1
  7.         for i in range(1, math.floor(n**0.5) + 1):
  8.             if n % i == 0:
  9.                 if i != n // i:
  10.                     counterparts.append(n // i)
  11.                 divisors.append(i)
  12.        
  13.         for _ in range(len(counterparts)):
  14.             cp = counterparts.pop()
  15.             divisors.append(cp)
  16.        
  17.         if k - 1 < len(divisors):
  18.             res = divisors[k-1]
  19.        
  20.         return res
Advertisement
Add Comment
Please, Sign In to add comment