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.
Examples :
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 :
C++
#include<iostream>
using namespace std;
int findKHCF( int x, int y, int k)
{
int small = min(x, y);
int count = 1;
for ( int i=2; i<=small; i++)
{
if (x % i==0 && y % i == 0)
count++;
if (count == k)
return i;
}
return -1;
}
int main()
{
int x = 4, y = 24, k = 3;
cout << findKHCF(x, y, k);
return 0;
}
|
Java
import java.lang.*;
class GFG {
static int findKHCF( int x, int y, int k) {
int small = Math.min(x, y);
int count = 1 ;
for ( int i = 2 ; i <= small; i++) {
if (x % i == 0 && y % i == 0 )
count++;
if (count == k)
return i;
}
return - 1 ;
}
public static void main(String[] args) {
int x = 4 , y = 24 , k = 3 ;
System.out.print(findKHCF(x, y, k));
}
}
|
Python3
def findKHCF(x,y,k):
small = min (x, y)
count = 1
for i in range ( 2 ,small + 1 ):
if (x % i = = 0 and y % i = = 0 ):
count = count + 1
if (count = = k):
return i
return - 1
x = 4
y = 24
k = 3
print (findKHCF(x, y, k))
|
C#
using System;
class GFG {
static int findKHCF( int x, int y, int k)
{
int small = Math.Min(x, y);
int count = 1;
for ( int i = 2; i <= small; i++)
{
if (x % i == 0 && y % i == 0)
count++;
if (count == k)
return i;
}
return -1;
}
public static void Main()
{
int x = 4, y = 24, k = 3;
Console.Write(findKHCF(x, y, k));
}
}
|
PHP
<?php
function findKCF( $x , $y , $k )
{
$small = min( $x , $y );
$count = 1;
for ( $i = 2; $i <= $small ; $i ++)
{
if ( $x % $i == 0 && $y % $i == 0)
$count ++;
if ( $count == $k )
return $i ;
}
return -1;
}
$x = 4; $y = 24; $k = 3;
echo findKCF( $x , $y , $k );
?>
|
Javascript
<script>
function findKHCF(x, y, k) {
let small = Math.min(x, y);
let count = 1;
for (let i = 2; i <= small; i++) {
if (x % i == 0 && y % i == 0)
count++;
if (count == k)
return i;
}
return -1;
}
let x = 4, y = 24, k = 3;
document.write(findKHCF(x, y, k));
</script>
|
Time complexity: O(n) where n is smallest among (x,y)
Auxiliary space: O(1)
If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!