📌  相关文章
📜  求出满足给定条件的给定输入整数N的最大和(a + b)

📅  最后修改于: 2021-04-24 03:43:44             🧑  作者: Mango

给定整数N ,任务是找到最大和( a + b ){1≤a≤N,1≤b≤N},使得a * b /(a + b)是整数(即a + b将a * b)和a!= b相除。

例子:

天真的方法:用天真的方法解决此问题的想法是使用嵌套循环的概念。可以按照以下步骤计算结果:

  1. 从1到N运行嵌套循环。
  2. 对于[1,N]范围内的每个数字a ,找到另一个整数b ,使a = b,并且( a + b )除a * b
  3. 如果满足条件,则将( a + b )的值存储在变量中,以跟踪获得的最大值。
  4. 最后,返回( a + b )的最大值。

下面是上述方法的实现:

C++
// C++ implementation to find the largest value
// of a + b satisfying the given condition
#include 
using namespace std;
  
// Function to return the maximum sum of
// a + b satisfying the given condition
int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0;
  
    // Consider all the possible pairs
    for (int i = 1; i <= N; i++) {
        for (int j = i + 1; j <= N; j++) {
  
            // Check if the product is
            // divisible by the sum
            if (i * j % (i + j) == 0)
  
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = max(max_sum, i + j);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
int main()
{
    int N = 25;
  
    int max_sum = getLargestSum(N);
  
    cout << max_sum << endl;
    return 0;
}


Java
// Java implementation to find the largest value
// of a + b satisfying the given condition 
import java.util.*;
   
class GFG{
    
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0;
  
    // Consider all the possible pairs
    for (int i = 1; i <= N; i++) {
        for (int j = i + 1; j <= N; j++) {
  
            // Check if the product is
            // divisible by the sum
            if (i * j % (i + j) == 0)
  
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = Math.max(max_sum, i + j);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 25;
  
    int max_sum = getLargestSum(N);
  
    System.out.print(max_sum );
}
}
  
// This code is contributed by shivanisinghss2110


Python3
# Python3 implementation to find the largest value
# of a + b satisfying the given condition
  
# Function to return the maximum sum of
# a + b satisfying the given condition
def getLargestSum(N):
    # Initialize max_sum
    max_sum = 0
  
    # Consider all the possible pairs
    for i in range(1,N+1):
        for j in range(i + 1, N + 1, 1):
  
            # Check if the product is
            # divisible by the sum
            if (i * j % (i + j) == 0):
  
                # Storing the maximum sum
                # in the max_sum variable
                max_sum = max(max_sum, i + j)
  
    # Return the max_sum value
    return max_sum
  
# Driver code
if __name__ == '__main__':
    N = 25
  
    max_sum = getLargestSum(N)
    print(max_sum)
  
# This code is contributed by Surendra_Gangwar


C#
// C# implementation to find the largest value 
// of a + b satisfying the given condition 
using System;
  
class GFG{ 
      
    // Function to return the maximum sum of 
    // a + b satisfying the given condition 
    static int getLargestSum(int N) 
    { 
        // Initialize max_sum 
        int max_sum = 0; 
      
        // Consider all the possible pairs 
        for (int i = 1; i <= N; i++) { 
            for (int j = i + 1; j <= N; j++) { 
      
                // Check if the product is 
                // divisible by the sum 
                if (i * j % (i + j) == 0) 
      
                    // Storing the maximum sum 
                    // in the max_sum variable 
                    max_sum = Math.Max(max_sum, i + j); 
            } 
        } 
      
        // Return the max_sum value 
        return max_sum; 
    } 
      
    // Driver code 
    public static void Main(string[] args) 
    { 
        int N = 25; 
      
        int max_sum = getLargestSum(N); 
      
        Console.WriteLine(max_sum ); 
    } 
} 
  
// This code is contributed by AnkitRai01


C++
// C++ implementation to  find the largest value
// of a + b satisfying the given condition
#include 
using namespace std;
  
// Function to return the maximum sum of
// a + b satisfying the given condition
int getLargestSum(int N)
{
    int max_sum = 0; // Initialize max_sum
  
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
        for (int j = i + 1; j * j <= N; j++) {
  
            // To find the largest factor k
            int k = N / j;
            int a = k * i;
            int b = k * j;
  
            // Check if the product is
            // divisible by the sum
            if (a <= N && b <= N
                && a * b % (a + b) == 0)
  
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = max(max_sum, a + b);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
int main()
{
    int N = 25;
    int max_sum = getLargestSum(N);
  
    cout << max_sum << endl;
    return 0;
}


Java
// Java implementation to find the largest value
// of a + b satisfying the given condition
  
class GFG{
  
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0; 
  
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
         for (int j = i + 1; j * j <= N; j++) {
  
              // To find the largest factor k
              int k = N / j;
              int a = k * i;
              int b = k * j;
  
               // Check if the product is
               // divisible by the sum
               if (a <= N && b <= N &&
                   a * b % (a + b) == 0)
  
                   // Storing the maximum sum
                   // in the max_sum variable
                   max_sum = Math.max(max_sum, a + b);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
    System.out.print(max_sum + "\n");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the largest value 
# of a + b satisfying the given condition 
  
# Function to return the maximum sum of 
# a + b satisfying the given condition 
def getLargestSum(N) : 
  
    max_sum = 0; # Initialize max_sum 
  
    # Consider all possible pairs and check 
    # the sum divides product property 
    for i in range(1, int(N ** (1/2))+1) :
        for j in range(i + 1, int(N ** (1/2)) + 1) :
              
            # To find the largest factor k
            k = N // j; 
            a = k * i;
            b = k * j;
              
            # Check if the product is
            # divisible by the sum
            if (a <= N and b <= N and a * b % (a + b) == 0) :
                # Storing the maximum sum
                # in the max_sum variable 
                max_sum = max(max_sum, a + b);
                  
    # Return the max_sum value
    return max_sum;
      
# Driver code
if __name__ == "__main__" :
    N = 25;
    max_sum = getLargestSum(N);
    print(max_sum);
  
# This code is contributed by AnkitRai01


C#
// C# implementation to find the largest value
// of a + b satisfying the given condition
using System;
  
class GFG{
  
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
      
    // Initialize max_sum
    int max_sum = 0; 
  
    // Consider all possible pairs and check
    // the sum divides product property
    for(int i = 1; i * i <= N; i++) 
    {
       for(int j = i + 1; j * j <= N; j++)
       {
          // To find the largest factor k
          int k = N / j;
          int a = k * i;
          int b = k * j;
            
          // Check if the product is
          // divisible by the sum
          if (a <= N && b <= N &&
              a * b % (a + b) == 0)
                
              // Storing the maximum sum
              // in the max_sum variable
              max_sum = Math.Max(max_sum, a + b);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
static public void Main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
      
    Console.Write(max_sum + "\n");
}
}
  
// This code is contributed by gauravrajput1


输出:
36

时间复杂度:由于嵌套的for循环运行(N *(N + 1))/ 2次,因此上述解决方案的时间复杂度为O(N 2 )

高效的方法:可以观察到的是,如果存在两个数字ab ,使得它们的乘积可以被它们的和除,那么它们就不是相对质数,即,它们的GCD不是一个。这可以使用欧几里得算法来证明。

因此,通过使用上述观察,可以按照以下步骤计算结果:

  1. 如果a可以表示为k * xb可以表示为k * y使得xy是互质数,则:
    a + b = k(x + y)
    a * b = k2
    
  2. 现在,将上述值相除:
    (a * b) /(a + b) = k * ((x * y)/(x + y)). 
    
  3. 因为已知x和y是互质的,所以( x * y )将不能被( x + y )整除。这意味着k必须可以被x + y整除。
  4. 因此, k是( k * x )和( k * y )小于N的最大可能因子。
  5. 显然,可被(x + y)整除的k的最小值为( x + y )。这意味着(x + y)* y&leq; N和(x + y)* x&leq; N.
  6. 因此,检查所有因素从1到N 0.5

下面是上述方法的实现:

C++

// C++ implementation to  find the largest value
// of a + b satisfying the given condition
#include 
using namespace std;
  
// Function to return the maximum sum of
// a + b satisfying the given condition
int getLargestSum(int N)
{
    int max_sum = 0; // Initialize max_sum
  
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
        for (int j = i + 1; j * j <= N; j++) {
  
            // To find the largest factor k
            int k = N / j;
            int a = k * i;
            int b = k * j;
  
            // Check if the product is
            // divisible by the sum
            if (a <= N && b <= N
                && a * b % (a + b) == 0)
  
                // Storing the maximum sum
                // in the max_sum variable
                max_sum = max(max_sum, a + b);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
int main()
{
    int N = 25;
    int max_sum = getLargestSum(N);
  
    cout << max_sum << endl;
    return 0;
}

Java

// Java implementation to find the largest value
// of a + b satisfying the given condition
  
class GFG{
  
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
    // Initialize max_sum
    int max_sum = 0; 
  
    // Consider all possible pairs and check
    // the sum divides product property
    for (int i = 1; i * i <= N; i++) {
         for (int j = i + 1; j * j <= N; j++) {
  
              // To find the largest factor k
              int k = N / j;
              int a = k * i;
              int b = k * j;
  
               // Check if the product is
               // divisible by the sum
               if (a <= N && b <= N &&
                   a * b % (a + b) == 0)
  
                   // Storing the maximum sum
                   // in the max_sum variable
                   max_sum = Math.max(max_sum, a + b);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
    System.out.print(max_sum + "\n");
}
}
  
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation to find the largest value 
# of a + b satisfying the given condition 
  
# Function to return the maximum sum of 
# a + b satisfying the given condition 
def getLargestSum(N) : 
  
    max_sum = 0; # Initialize max_sum 
  
    # Consider all possible pairs and check 
    # the sum divides product property 
    for i in range(1, int(N ** (1/2))+1) :
        for j in range(i + 1, int(N ** (1/2)) + 1) :
              
            # To find the largest factor k
            k = N // j; 
            a = k * i;
            b = k * j;
              
            # Check if the product is
            # divisible by the sum
            if (a <= N and b <= N and a * b % (a + b) == 0) :
                # Storing the maximum sum
                # in the max_sum variable 
                max_sum = max(max_sum, a + b);
                  
    # Return the max_sum value
    return max_sum;
      
# Driver code
if __name__ == "__main__" :
    N = 25;
    max_sum = getLargestSum(N);
    print(max_sum);
  
# This code is contributed by AnkitRai01

C#

// C# implementation to find the largest value
// of a + b satisfying the given condition
using System;
  
class GFG{
  
// Function to return the maximum sum of
// a + b satisfying the given condition
static int getLargestSum(int N)
{
      
    // Initialize max_sum
    int max_sum = 0; 
  
    // Consider all possible pairs and check
    // the sum divides product property
    for(int i = 1; i * i <= N; i++) 
    {
       for(int j = i + 1; j * j <= N; j++)
       {
          // To find the largest factor k
          int k = N / j;
          int a = k * i;
          int b = k * j;
            
          // Check if the product is
          // divisible by the sum
          if (a <= N && b <= N &&
              a * b % (a + b) == 0)
                
              // Storing the maximum sum
              // in the max_sum variable
              max_sum = Math.Max(max_sum, a + b);
        }
    }
  
    // Return the max_sum value
    return max_sum;
}
  
// Driver code
static public void Main(String[] args)
{
    int N = 25;
    int max_sum = getLargestSum(N);
      
    Console.Write(max_sum + "\n");
}
}
  
// This code is contributed by gauravrajput1
输出:
36

时间复杂度:尽管有两个循环,但每个循环最多运行sqrt(N)时间。因此,整体时间复杂度O(N)