Kth largest factor of N
Write a program to find the Kth largest factor of a number N.
Input
The first line is an integer N. The second line is an integer K.
Output
Print Kth largest factor of N. Print 1, If the number of factors of N is less than K.
Explanation
For
N = 12 and K = 3 The factors of 12 are 1, 2, 3, 4, 6, 12. The 3rd largest factor is 4.For
N = 12 and K = 7 The number of factors for 12 is only 6 and the given K is 7 (no of factors of N < K). Hence the output should be 1.
Sample Input 1
12
3
Sample Output 1
4
Sample Input 2
12
7
Sample Output 2
1
n = int(input())
k = int(input())
factors = []
for i in range(n):
if (n % (i+1)) == 0:
factors.append(i+1)
if k > len(factors):
print(1)
else:
print(factors[-k])
Comments
Leave a comment