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.
 
Example 1:
Input: n = 12, k = 3
Output: 3
Explanation: Factors list is [1, 2, 3, 4, 6, 12], the 3rd factor is 3.

Example 2:
Input: n = 7, k = 2
Output: 7
Explanation: Factors list is [1, 7], the 2nd factor is 7.

Example 3:
Input: n = 4, k = 4
Output: -1
Explanation: Factors list is [1, 2, 4], there is only 3 factors. We should return -1.

Example 4:
Input: n = 1, k = 1
Output: 1
Explanation: Factors list is [1], the 1st factor is 1.

Example 5:
Input: n = 1000, k = 3
Output: 4
Explanation: Factors list is [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500, 1000].

 
Constraints:

1 <= k <= n <= 1000

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: Mathematics

Starter code: class Solution:
    def kthFactor(self, n: int, k: int) -> int:
        

Solutions:
class Solution:

	def kthFactor(self, n: int, k: int) -> int:
		i = 0
		for j in range(1, n + 1):
			if n % j == 0:
				i += 1
				if i == k:
					return j
		return -1


class Solution:

	def kthFactor(self, n: int, k: int) -> int:
		r1 = []
		r2 = []
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				r1.append(i)
				if n != i * i:
					r2 = [n // i] + r2
		r = r1 + r2
		if len(r) < k:
			return -1
		else:
			return r[k - 1]


class Solution:

	def kthFactor(self, n: int, k: int) -> int:
		i = 1
		factor_list_small = []
		factor_list_large = []
		while pow(i, 2) < n:
			(a, b) = divmod(n, i)
			if b == 0:
				factor_list_small.append(i)
				factor_list_large = [a] + factor_list_large
			i += 1
		if pow(i, 2) == n:
			factor_list_small.append(i)
		factor_list = factor_list_small + factor_list_large
		if len(factor_list) < k:
			return -1
		else:
			return factor_list[k - 1]


from math import sqrt, ceil

class Solution:

	def kthFactor(self, n, k):
		factors = []
		if int(sqrt(n)) == sqrt(n):
			for i in range(1, int(sqrt(n)) + 1):
				if n % i == 0:
					factors.append(i)
					if i == int(n / i):
						continue
					factors.append(int(n / i))
		else:
			for i in range(1, ceil(sqrt(n))):
				if n % i == 0:
					factors.append(i)
					factors.append(int(n / i))
		factors.sort()
		if k > len(factors):
			return -1
		return factors[k - 1]


import math

class Solution:

	def kthFactor(self, n: int, k: int) -> int:
		facts = []
		for i in range(1, n + 1):
			if n % i == 0:
				facts.append(i)
		if len(facts) < k:
			return -1
		else:
			return facts[k - 1]


class Solution:

	def kthFactor(self, n: int, k: int) -> int:
		check = []
		for i in range(1, n + 1):
			if n % i == 0:
				check.append(i)
		if len(check) < k:
			return -1
		else:
			return check[k - 1]


class Solution:

	def kthFactor(self, n, k):
		solution_space = list([x for x in list(range(1, n + 1)) if n % x == 0])
		if len(solution_space) < k:
			return -1
		else:
			return list([x for x in list(range(1, n + 1)) if n % x == 0])[k - 1]


class Solution:

	def kthFactor(self, n: int, k: int) -> int:
		try:
			return list(filter(lambda x: n % x == 0, range(1, n + 1)))[k - 1]
		except:
			return -1