原文:

给定三个数字 x,y 和 k,求 x 和 y 的第 k 个公因数,如果 x 和 y 的公因数小于 k,则打印-1 . 例:

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

我们发现两个数中较小的一个作为公因数不能大于较小的一个数。然后我们运行一个从 1 到较小数字的循环。对于每个数字 i,我们检查它是否是一个共同因素。如果是,我们增加公共因子的计数。 下面是实现:

c

// c   program to find kth common factor
// of two numbers
#include
using namespace std;
// returns k'th common factor of x and y.
int findkcf(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.

python 3

# 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.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

4

本文由阿夫扎尔·安萨里供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 contribute@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。