📜  查找具有给定GCD和LCM的任何一对

📅  最后修改于: 2021-04-25 00:50:37             🧑  作者: Mango

给定gcd G和lcmL。任务是打印具有gcd G和lcm L的任何一对。
例子:

Input: G = 3, L = 12 
Output: 3, 12

Input: G = 1, L = 10 
Output: 1, 10

正常的解决方案是对所有因子对g * l执行迭代,并检查是否有任何对的gcd g和lcm为l。如果他们有,那么这对就是答案。
下面是上述方法的实现。

C++
// C++ program to print any pair
// with a given gcd G and lcm L
#include 
using namespace std;
 
// Function to print the pairs
void printPair(int g, int l)
{
    int n = g * l;
 
    // iterate over all factor pairs
    for (int i = 1; i * i <= n; i++) {
 
        // check if a factor
        if (n % i == 0) {
            int first = i;
            int second = n / i;
 
            // find gcd
            int gcd = __gcd(first, second);
 
            // check if gcd is same as given g
            // and lcm is same as lcm l
            if (gcd == g && l % first == 0 && l % second == 0) {
                cout << first << " " << second;
                return;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int g = 3, l = 12;
    printPair(g, l);
    return 0;
}


Java
// Java program to print any pair
// with a given gcd G and lcm L
 
import java.math.BigInteger;
 
class GFG {
 
// Function to print the pairs
    static void printPair(int g, int l) {
        int n = g * l;
 
        // iterate over all factor pairs
        for (int i = 1; i * i <= n; i++) {
 
            // check if a factor
            if (n % i == 0) {
                int first = i;
                int second = n / i;
 
                // find gcd
                int gcd = __gcd(first, second);
 
                // check if gcd is same as given g
                // and lcm is same as lcm l
                if (gcd == g && l % first == 0 && l % second == 0) {
                    System.out.println(first + " " + second);
                    return;
                }
            }
        }
    }
//Function return GCD of two give number
 
    private static int __gcd(int a, int b) {
        // there's a better way to do this. I forget.
        BigInteger b1 = new BigInteger("" + a);
        BigInteger b2 = new BigInteger("" + b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }
// Driver function
 
    public static void main(String[] args) {
        int g = 3, l = 12;
        printPair(g, l);
 
    }
}
// This code is contributed by RAJPUT-JI


Python3
# Python program to print any pair
# with a given gcd G and lcm L
  
# Function to print the pairs
def printPair(g, l):
    n = g * l;
  
    # iterate over all factor pairs
    for i in range(1,n+1):
  
        # check if a factor
        if (n % i == 0):
            first = i;
            second = n // i;
  
            # find gcd
            gcd = __gcd(first, second);
  
            # check if gcd is same as given g
            # and lcm is same as lcm l
            if (gcd == g and l % first == 0 and
                              l % second == 0):
                print(first , " " , second);
                return;
 
  
# Function return GCD of two give number
def __gcd(a, b):
    if(b==0):
        return a;
    else:
        return __gcd(b, a % b);
  
# Driver Code
g = 3;
l = 12;
printPair(g, l);
 
# This code is contributed by Princi Singh


C#
// C# program to print any pair
// with a given gcd G and lcm L
using System;
public class GFG {
 
// Function to print the pairs
    static void printPair(int g, int l) {
        int n = g * l;
 
        // iterate over all factor pairs
        for (int i = 1; i * i <= n; i++) {
 
            // check if a factor
            if (n % i == 0) {
                int first = i;
                int second = n / i;
 
                // find gcd
                int gcd = __gcd(first, second);
 
                // check if gcd is same as given g
                // and lcm is same as lcm l
                if (gcd == g && l % first == 0 && l % second == 0) {
                    Console.WriteLine(first + " " + second);
                    return;
                }
            }
        }
    }
//Function return GCD of two give number
 
    private static int __gcd(int a, int b) {
        return b == 0 ? a : __gcd(b, a % b);
    }
// Driver function
 
    public static void Main() {
        int g = 3, l = 12;
        printPair(g, l);
 
    }
}
 
// This code is contributed by RAJPUT-JI


PHP


Javascript


C++
// C++ program to print any pair
// with a given gcd G and lcm L
#include 
using namespace std;
 
// Function to print the pairs
void printPair(int g, int l)
{
    cout << g << " " << l;
}
 
// Driver Code
int main()
{
    int g = 3, l = 12;
    printPair(g, l);
    return 0;
}


Java
// Java program to print any pair
// with a given gcd G and lcm L
 
import java.io.*;
 
class GFG {
     
 
 
// Function to print the pairs
 static void printPair(int g, int l)
{
    System.out.print( g + " " + l);
}
 
// Driver Code
    public static void main (String[] args) {
    int g = 3, l = 12;
    printPair(g, l);
    }
}
// This code is contributed by inder_verma.


Python 3
# Python 3 program to print any pair
# with a given gcd G and lcm L
 
# Function to print the pairs
def printPair(g, l):
    print(g, l)
 
# Driver Code
g = 3; l = 12;
printPair(g, l);
 
# This code is contributed
# by Akanksha Rai


C#
// C# program to print any pair
// with a given gcd G and lcm L
using System;
 
class GFG
{
     
// Function to print the pairs
static void printPair(int g, int l)
{
    Console.Write( g + " " + l);
}
 
// Driver Code
public static void Main ()
{
    int g = 3, l = 12;
    printPair(g, l);
}
}
 
// This code is contributed
// by Subhadeep


PHP


Javascript


输出:

3 12

时间复杂度: O(sqrt(g * l))
一个有效的解决方案是观察lcm总是被gcd整除,因此答案可以在O(1)中获得。其中一个数字是gcd G本身,另一个是lcmL。
下面是上述方法的实现。

C++

// C++ program to print any pair
// with a given gcd G and lcm L
#include 
using namespace std;
 
// Function to print the pairs
void printPair(int g, int l)
{
    cout << g << " " << l;
}
 
// Driver Code
int main()
{
    int g = 3, l = 12;
    printPair(g, l);
    return 0;
}

Java

// Java program to print any pair
// with a given gcd G and lcm L
 
import java.io.*;
 
class GFG {
     
 
 
// Function to print the pairs
 static void printPair(int g, int l)
{
    System.out.print( g + " " + l);
}
 
// Driver Code
    public static void main (String[] args) {
    int g = 3, l = 12;
    printPair(g, l);
    }
}
// This code is contributed by inder_verma.

的Python 3

# Python 3 program to print any pair
# with a given gcd G and lcm L
 
# Function to print the pairs
def printPair(g, l):
    print(g, l)
 
# Driver Code
g = 3; l = 12;
printPair(g, l);
 
# This code is contributed
# by Akanksha Rai

C#

// C# program to print any pair
// with a given gcd G and lcm L
using System;
 
class GFG
{
     
// Function to print the pairs
static void printPair(int g, int l)
{
    Console.Write( g + " " + l);
}
 
// Driver Code
public static void Main ()
{
    int g = 3, l = 12;
    printPair(g, l);
}
}
 
// This code is contributed
// by Subhadeep

的PHP


Java脚本


输出:

3 12

时间复杂度: O(1)