📜  前 N 个自然数的所有对 (i, j) 中的最大 GCD

📅  最后修改于: 2021-10-25 06:24:40             🧑  作者: Mango

给定一个正整数N > 1 ,任务是在所有对(i, j) 中找到最大的 GCD 使得i < j < N
例子:

朴素方法:从范围[1, N]生成所有可能的整数对并计算所有对的 GCD。最后,打印得到的最大 GCD。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find maximum gcd
// of all pairs possible from
// first n natural numbers
int maxGCD(int n)
{
    // Stores maximum gcd
    int maxHcf = INT_MIN;
 
    // Iterate over all possible pairs
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
 
            // Update maximum GCD
            maxHcf
                = max(maxHcf, __gcd(i, j));
        }
    }
 
    return maxHcf;
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << maxGCD(n);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to find maximum gcd
// of all pairs possible from
// first n natural numbers
static int maxGCD(int n)
{
  // Stores maximum gcd
  int maxHcf = Integer.MIN_VALUE;
 
  // Iterate over all possible pairs
  for (int i = 1; i <= n; i++)
  {
    for (int j = i + 1; j <= n; j++)
    {
      // Update maximum GCD
      maxHcf = Math.max(maxHcf,
                        __gcd(i, j));
    }
  }
 
  return maxHcf;
}
   
static int __gcd(int a, int b) 
{ 
  return b == 0 ? a :
         __gcd(b, a % b);    
}
   
// Driver Code
public static void main(String[] args)
{
  int n = 4;
  System.out.print(maxGCD(n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for
# the above approach
def __gcd(a, b):
    if(b == 0):
        return a;
    else:
        return __gcd(b, a % b);
 
# Function to find maximum gcd
# of all pairs possible from
# first n natural numbers
def maxGCD(n):
   
    # Stores maximum gcd
    maxHcf = -2391734235435;
 
    # Iterate over all possible pairs
    for i in range(1, n + 1):
        for j in range(i + 1, n + 1):
           
            # Update maximum GCD
            maxHcf = max(maxHcf, __gcd(i, j));
    return maxHcf;
 
# Driver Code
if __name__ == '__main__':
    n = 4;
    print(maxGCD(n));
 
# This code is contributed by gauravrajput1


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to find maximum gcd
// of all pairs possible from
// first n natural numbers
static int maxGCD(int n)
{
  // Stores maximum gcd
  int maxHcf = int.MinValue;
 
  // Iterate over all possible pairs
  for (int i = 1; i <= n; i++)
  {
    for (int j = i + 1; j <= n; j++)
    {
      // Update maximum GCD
      maxHcf = Math.Max(maxHcf,
                        __gcd(i, j));
    }
  }
 
  return maxHcf;
}
   
static int __gcd(int a, int b) 
{ 
  return b == 0 ? a :
         __gcd(b, a % b);    
}
   
// Driver Code
public static void Main(String[] args)
{
  int n = 4;
  Console.Write(maxGCD(n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
int maxGCD(int n)
{
 
    // Return max GCD
    return (n / 2);
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << maxGCD(n);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG{
 
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
     
    // Return max GCD
    return (n / 2);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 4;
 
    System.out.print(maxGCD(n));
}
}
 
// This code is contributed by Dewanti


Python3
# Python3 implementation of the
# above approach
 
# Function to find the maximum GCD
# among all the pairs from first n
# natural numbers
def maxGCD(n):
 
    # Return max GCD
    return (n // 2);
 
# Driver code
if __name__ == '__main__':
     
    n = 4;
 
    print(maxGCD(n));
 
# This code is contributed by Amit Katiyar


C#
// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
  // Return max GCD
  return (n / 2);
}
 
// Driver code
public static void Main(String[] args)
{
  int n = 4;
  Console.Write(maxGCD(n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

2

时间复杂度: O(N 2 log N)
辅助空间: O(1)
有效的方法: NN / 2的 GCD 是N / 2 ,这是从 1 到N 的任何对的所有 GCD 可能的最大值。

下面是上述方法的实现:

C++

// C++ implementation of the above approach
 
#include 
using namespace std;
 
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
int maxGCD(int n)
{
 
    // Return max GCD
    return (n / 2);
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << maxGCD(n);
 
    return 0;
}

Java

// Java implementation of the above approach
class GFG{
 
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
     
    // Return max GCD
    return (n / 2);
}
 
// Driver code
public static void main(String[] args)
{
    int n = 4;
 
    System.out.print(maxGCD(n));
}
}
 
// This code is contributed by Dewanti

蟒蛇3

# Python3 implementation of the
# above approach
 
# Function to find the maximum GCD
# among all the pairs from first n
# natural numbers
def maxGCD(n):
 
    # Return max GCD
    return (n // 2);
 
# Driver code
if __name__ == '__main__':
     
    n = 4;
 
    print(maxGCD(n));
 
# This code is contributed by Amit Katiyar

C#

// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to find the maximum GCD
// among all the pairs from
// first n natural numbers
static int maxGCD(int n)
{
  // Return max GCD
  return (n / 2);
}
 
// Driver code
public static void Main(String[] args)
{
  int n = 4;
  Console.Write(maxGCD(n));
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
2

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