Open In App

Print the kth common factor of two numbers

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to find kth common factor
// of two numbers
#include<iostream>
using namespace std;
 
// Returns k'th common factor of x and y.
int findKHCF(int x, int y, int k)
{
   // Find smaller of two numbers
   int small = min(x, y);
 
   // Count common factors until we either
   // reach small or count becomes k.
   int count = 1;
   for (int i=2; i<=small; i++)
   {
      if (x % i==0 && y % i == 0)
         count++;
      if (count == k)
         return i;
   }
 
   // If we reached small
   return -1;
}
 
// Driver code
int main()
{
   int x = 4, y = 24, k = 3;
   cout << findKHCF(x, y, k);
   return 0;
}


Java




// Java program to find kth
// common factor of two numbers
import java.lang.*;
 
class GFG {
     
// Returns k'th common factor of x and y.
static int findKHCF(int x, int y, int k) {
     
    // Find smaller of two numbers
    int small = Math.min(x, y);
 
    // Count common factors until we either
    // reach small or count becomes k.
    int count = 1;
    for (int i = 2; i <= small; i++) {
    if (x % i == 0 && y % i == 0)
        count++;
    if (count == k)
        return i;
    }
 
    // If we reached small
    return -1;
}
 
// Driver code
public static void main(String[] args) {
     
    int x = 4, y = 24, k = 3;
    System.out.print(findKHCF(x, y, k));
}
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python program to find
# kth common factor
# of two numbers
 
# Returns k'th common
# factor of x and y.
def findKHCF(x,y,k):
 
    # Find smaller of two numbers
    small = min(x, y)
  
    # Count common factors
    # until we either
    # reach small or count
    # becomes k.
    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
    
  
    # If we reached small
    return -1
 
# Driver code
 
x = 4
y = 24
k = 3
print(findKHCF(x, y, k))
 
# This code is contributed
# by Anant Agarwal.


C#




// C# program to find kth
// common factor of two numbers
using System;
 
class GFG {
     
// Returns k'th common factor of x and y.
static int findKHCF(int x, int y, int k)
{
     
    // Find smaller of two numbers
    int small = Math.Min(x, y);
 
    // Count common factors until we either
    // reach small or count becomes k.
    int count = 1;
    for (int i = 2; i <= small; i++)
    {
        if (x % i == 0 && y % i == 0)
            count++;
        if (count == k)
            return i;
    }
 
    // If we reached small
    return -1;
}
 
// Driver code
public static void Main()
{
    int x = 4, y = 24, k = 3;
    Console.Write(findKHCF(x, y, k));
}
}
 
// This code is contributed by Nitin Mittal.


PHP




<?php
// PHP program to find kth
// common factor of two numbers
 
// Returns k'th common
// factor of x and y.
function findKCF($x, $y, $k)
{
// Find smaller of two numbers
$small = min($x, $y);
 
// Count common factors until we either
// reach small or count becomes k.
$count = 1;
for ($i = 2; $i <= $small; $i++)
{
    if ($x % $i == 0 && $y % $i == 0)
        $count++;
    if ($count == $k)
        return $i;
}
 
// If we reached small
return -1;
}
 
// Driver code
$x = 4; $y = 24; $k = 3;
echo findKCF($x, $y, $k);
 
// This code is contributed by nitin mittal.
?>


Javascript




<script>
 
// Javascript program to find kth
// common factor of two numbers
 
// Returns k'th common factor of x and y.
function findKHCF(x, y, k) {
       
    // Find smaller of two numbers
    let small = Math.min(x, y);
   
    // Count common factors until we either
    // reach small or count becomes k.
    let count = 1;
    for (let i = 2; i <= small; i++) {
    if (x % i == 0 && y % i == 0)
        count++;
    if (count == k)
        return i;
    }
   
    // If we reached small
    return -1;
}
      
// Driver code   
 
    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)

 



Similar Reads

Count of numbers up to N having at least one prime factor common with N
Given an integer N, the task is to count the number of integers from the range [1, N] having at least one in common prime factor with N other than 1. Examples: Input: N = 5Output: 1Explanation: Since 5 is prime. Therefore, there is no other number which is at most N, that has at least one common factor with N except N itself. Therefore, the count i
10 min read
Program to find HCF (Highest Common Factor) of 2 Numbers
HCF (Highest Common Factor) or GCD (Greatest Common Divisor) of two numbers is the largest number that divides both of them.  For example, GCD of 20 and 28 is 4, and GCD of 98 and 56 is 14. Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.  A simple solution is to find all prime factors of both numbers, then find
8 min read
Kth largest factor of number N
Given two positive integers N and K, the task is to print the Kth largest factor of N. Input: N = 12, K = 3Output: 4Explanation: The factors of 12 are {1, 2, 3, 4, 6, 12}. The largest factor is 12 and the 3rd largest factor is 4. Input: N = 30, K = 2Output: 15Explanation: The factors of 30 are {1, 2, 3, 5, 6, 10, 15, 30} and the 2nd largest factor
12 min read
Print common nodes on path from root (or common ancestors)
Given a binary tree and two nodes, the task is to Print all the nodes that are common for 2 given nodes in a binary tree. Examples: Given binary tree is : 1 / \ 2 3 / \ / \ 4 5 6 7 / / \ 8 9 10 Given nodes 9 and 7, so the common nodes are:- 1, 3 Asked in : Amazon Find the LCA of given two nodes.Print all ancestors of the LCA as done in this post, a
11 min read
Maximum length subsequence such that adjacent elements in the subsequence have a common factor
Given an array arr[], the task is to find the maximum length of a subsequence such that the adjacent elements in the subsequence have a common factor. Examples: Input: arr[] = { 13, 2, 8, 6, 3, 1, 9 } Output: 5Max length subsequence with satisfied conditions: { 2, 8, 6, 3, 9 } Input: arr[] = { 12, 2, 8, 6, 3, 1, 9 } Output: 6Max length subsequence
9 min read
Nearest element with at-least one common prime factor
Given an array arr[], find the nearest element for every element such that there is at least one common prime factor. In the output, we need to print the position of the closest element.Example: Input: arr[] = {2, 9, 4, 3, 13} Output: 3 4 1 2 -1 Explanation : Closest element for 1st element is 3rd. =&gt;Common prime factor of 1st and 3rd elements i
18 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Print the path common to the two paths from the root to the two given nodes
Given a binary tree with distinct nodes(no two nodes have the same data values). The problem is to print the path common to the two paths from the root to the two given nodes n1 and n2. If either of the nodes are not present then print "No Common Path". Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 / \ 8 9 n1 = 4, n2 = 8 Output : 1-&gt;2 Path form ro
22 min read
Make two numbers equal in at most K steps dividing by their factor
Given integers X, Y, and K, the task is to make X and Y equal in not more than K operations by applying the following operations: Choose an integer A and make X = X/A, (where A is an integer that is greater than 1 and not equal to X).Choose an integer A and make Y = Y/A, (where A is an integer that is greater than 1 and not equal to Y). Examples: I
6 min read
Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.Examples: Input : N = 6 Output : 6 2 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The triangle formed has two sides (AB
5 min read
Article Tags :
Practice Tags :