📌  相关文章
📜  乘积为N的一对整数的最大可能GCD

📅  最后修改于: 2021-04-22 01:58:58             🧑  作者: Mango

给定一个整数N ,任务是在所有乘积N的整数对中找到最大可能的GCD。
例子:

天真的方法:
解决此问题的最简单方法是用乘积N生成所有可能的对,并计算所有此类对的GCD。最后,打印获得的最大GCD。
时间复杂度: O(NlogN)
辅助空间: O(1)
高效方法:
通过找到给定数N的所有除数,可以优化上述方法。对于获得的每对除数,计算其GCD。最后,打印获得的最大GCD。
请按照以下步骤解决问题:

  • 声明一个变量maxGcd以跟踪最大GCD。
  • 迭代到√N ,对于每个整数,检查它是否为N的因数。
  • 如果N可被i整除,则计算这对因子(i,N / i)的GCD。
  • Comapare与GCD(i,N / i)并更新maxGcd
  • 最后,打印maxGcd

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to return
/// the maximum GCD
int getMaxGcd(int N)
{
    int maxGcd = INT_MIN, A, B;
 
    // To find all divisors of N
    for (int i = 1; i <= sqrt(N); i++) {
 
        // If i is a factor
        if (N % i == 0) {
            // Store the pair of factors
            A = i, B = N / i;
 
            // Store the maximum GCD
            maxGcd = max(maxGcd, __gcd(A, B));
        }
    }
 
    // Return the maximum GCD
    return maxGcd;
}
 
// Driver Code
int main()
{
 
    int N = 18;
    cout << getMaxGcd(N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to return
// the maximum GCD
static int getMaxGcd(int N)
{
    int maxGcd = Integer.MIN_VALUE, A, B;
     
    // To find all divisors of N
    for(int i = 1; i <= Math.sqrt(N); i++)
    {
         
        // If i is a factor
        if (N % i == 0)
        {
             
            // Store the pair of factors
            A = i;
            B = N / i;
         
            // Store the maximum GCD
            maxGcd = Math.max(maxGcd, gcd(A, B));
        }
    }
     
    // Return the maximum GCD
    return maxGcd;
}
     
// Driver Code
public static void main(String s[])
{
    int N = 18;
     
    System.out.println(getMaxGcd(N));
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program to implement
# the above approach
import sys
import math
 
# Function to return
# the maximum GCD
def getMaxGcd(N):
     
    maxGcd = -sys.maxsize - 1
 
    # To find all divisors of N
    for i in range(1, int(math.sqrt(N)) + 1):
 
        # If i is a factor
        if (N % i == 0):
             
            # Store the pair of factors
            A = i
            B = N // i
 
            # Store the maximum GCD
            maxGcd = max(maxGcd, math.gcd(A, B))
         
    # Return the maximum GCD
    return maxGcd
 
# Driver Code
N = 18
 
print(getMaxGcd(N))
 
# This code is contributed by code_hunt


C#
// C# program to implement
// the above approach
using System;
class GFG{
     
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to return
// the maximum GCD
static int getMaxGcd(int N)
{
    int maxGcd = int.MinValue, A, B;
     
    // To find all divisors of N
    for(int i = 1; i <= Math.Sqrt(N); i++)
    {
         
        // If i is a factor
        if (N % i == 0)
        {
             
            // Store the pair of factors
            A = i;
            B = N / i;
         
            // Store the maximum GCD
            maxGcd = Math.Max(maxGcd, gcd(A, B));
        }
    }
     
    // Return the maximum GCD
    return maxGcd;
}
     
// Driver Code
public static void Main(String []s)
{
    int N = 18;
     
    Console.WriteLine(getMaxGcd(N));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
3

时间复杂度: O(√N* log(N))
辅助空间: O(1)