📌  相关文章
📜  LCM和GCD之和等于N的所有可能对的计数

📅  最后修改于: 2021-04-29 14:52:04             🧑  作者: Mango

给定一个整数N ,任务是找到所有可能的整数对( A,B)的计数,以使GCD(A,B)+ LCM(A,B)=N。

例子:

方法:
请按照以下步骤解决问题:

  • 初始化变量count ,以存储所有可能的对的计数。
  • [1,N]范围内迭代以生成所有可能的对(i,j)。计算使用的(I,J)的__gcd()函数和计算LCM(I,J)的GCD。
  • 现在,检查LCM(i,j)GCD(i,j)的总和是否等于N。如果是这样,请增加count
  • 遍历范围后打印数值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate and
// return LCM of two numbers
int lcm(int a, int b)
{
    return (a * b) / __gcd(a, b);
}
 
// Function to count pairs
// whose sum of GCD and LCM
// is equal to N
int countPair(int N)
{
    int count = 0;
    for (int i = 1;
        i <= N; i++) {
 
        for (int j = 1;
            j <= N; j++) {
 
            if (__gcd(i, j)
                    + lcm(i, j)
                == N) {
 
                count++;
            }
        }
    }
 
    return count;
}
 
// Driver Code
int main()
{
    int N = 14;
    cout << countPair(N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);
}
 
// Function to calculate and
// return LCM of two numbers
static int lcm(int a, int b)
{
    return (a * b) / __gcd(a, b);
}
 
// Function to count pairs
// whose sum of GCD and LCM
// is equal to N
static int countPair(int N)
{
    int count = 0;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if (__gcd(i, j) + lcm(i, j) == N)
            {
                count++;
            }
        }
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 14;
     
    System.out.print(countPair(N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement 
# the above approach 
 
# Recursive function to return
# gcd of a and b 
def __gcd(a, b): 
     
    if b == 0:
        return a
    else:
        return __gcd(b, a % b)
     
# Function to calculate and 
# return LCM of two numbers 
def lcm(a, b):
 
    return (a * b) // __gcd(a, b) 
 
# Function to count pairs 
# whose sum of GCD and LCM 
# is equal to N 
def countPair(N): 
 
    count = 0
     
    for i in range(1, N + 1):
        for j in range(1, N + 1):
            if (__gcd(i, j) + lcm(i, j) == N): 
                count += 1 
             
    return count
 
# Driver code
if __name__=="__main__":
     
    N = 14 
       
    print(countPair(N))
 
# This code is contributed by rutvik_56


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);
}
 
// Function to calculate and
// return LCM of two numbers
static int lcm(int a, int b)
{
    return (a * b) / __gcd(a, b);
}
 
// Function to count pairs
// whose sum of GCD and LCM
// is equal to N
static int countPair(int N)
{
    int count = 0;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            if (__gcd(i, j) + lcm(i, j) == N)
            {
                count++;
            }
        }
    }
    return count;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 14;
 
    Console.Write(countPair(N));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
7

时间复杂度: O(N 3 )
辅助空间: O(1)