Answer to Question #226582 in Python for kaavya

Question #226582

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




1
Expert's answer
2021-08-16T05:48:57-0400
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])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS