Given three numbers x, y and k, find the k’th common factor of x and y. Print -1 if there are less than k common factors of x and y.
Input : x = 20, y = 24 k = 3 Output : 4 Common factors are 1, 2, 4, ... Input : x = 4, y = 24 k = 2 Output : 2 Input : x = 22, y = 2 k = 3 Output : -1
We find the smaller of two numbers as common factor cannot be greater than the smaller number. Then we run a loop from 1 to the smaller number. For every number i, we check if it is a common factor. If yes, we increment count of common factors.
Below is the Implementation :
This article is contributed by Afzal Ansari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to find HCF (Highest Common Factor) of 2 Numbers
- Nearest element with at-least one common prime factor
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Least prime factor of numbers till n
- Exactly n distinct prime factor numbers from a to b
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Count all the numbers in a range with smallest factor as K
- Common Divisors of Two Numbers
- Common divisors of N numbers
- Sum of common divisors of two numbers A and B
- C++ Program for Common Divisors of Two Numbers
- Common prime factors of two numbers
- Divide the two given numbers by their common divisors
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Java Program for Common Divisors of Two Numbers
- Program to find the common ratio of three numbers
- Count common prime factors of two numbers
- Count of common multiples of two numbers in a range
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Improved By : nitin mittal