📜  需要对两个给定数字之一执行的最小递增/递减数,以使其非互质

📅  最后修改于: 2022-05-13 01:56:09.673000             🧑  作者: Mango

需要对两个给定数字之一执行的最小递增/递减数,以使其非互质

给定两个正整数AB ,任务是找到需要在AB上执行的最小递增/递减数,以使这两个数字互质

例子:

方法:可以根据以下观察解决给定的问题:

  • 如果AB的最大公约数大于1 ,则不会执行递增或递减,因为数字已经是非互质的。
  • 现在,检查AB在两个方向上的差异1 。因此,只需一步即可将任何数转换为偶数。
  • 如果上述两种情况均不适用,则需要进行2 次递增/递减操作以使数字AB成为最接近的偶数,从而使数字变为非共素数。

根据以上观察,按照以下步骤解决问题:

  • 如果AB的 GCD 不等于1 ,则打印0 ,因为不需要任何操作。
  • 否则,如果 { {A + 1, B} , {A – 1, B} , {A, B + 1} , {A, B – 1} } 对之一的 GCD 不等于1 ,则打印1因为只需要一项操作。
  • 否则,打印2

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number of
// increments/decrements operations required
// to make both the numbers non-coprime
int makeCoprime(int a, int b)
{
    // If a & b are already non-coprimes
    if (__gcd(a, b) != 1)
        return 0;
 
    // If a & b can become non-coprimes
    // by only incrementing/decrementing
    // a number only once
    if (__gcd(a - 1, b) != 1
        or __gcd(a + 1, b) != 1
        or __gcd(b - 1, a) != 1
        or __gcd(b + 1, a) != 1)
        return 1;
 
    // Otherwise
    return 2;
}
 
// Driver Code
int main()
{
    int A = 7, B = 17;
    cout << makeCoprime(A, B);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
 
  // function to calculate gcd
  static int __gcd(int a, int b)
  {
     
    // Everything divides 0
    if (a == 0)
      return b;
    if (b == 0)
      return a;
 
    // base case
    if (a == b)
      return a;
 
    // a is greater
    if (a > b)
      return __gcd(a-b, b);
    return __gcd(a, b-a);
  }
 
  // Function to find the minimum number of
  // increments/decrements operations required
  // to make both the numbers non-coprime
  static int makeCoprime(int a, int b)
  {
 
    // If a & b are already non-coprimes
    if (__gcd(a, b) != 1)
      return 0;
 
    // If a & b can become non-coprimes
    // by only incrementing/decrementing
    // a number only once
    if (__gcd(a - 1, b) != 1 || __gcd(a + 1, b) != 1
        || __gcd(b - 1, a) != 1
        || __gcd(b + 1, a) != 1)
      return 1;
 
    // Otherwise
    return 2;
  }
 
  // Driver code
  public static void main(String args[])
  {
    int A = 7, B = 17;
    System.out.println(makeCoprime(A, B));
  }
}
 
// This code is contributed by SoumikMondal


Python3
# Python3 program for the above approach
from math import gcd
 
# Function to find the minimum number of
# increments/decrements operations required
# to make both the numbers non-coprime
def makeCoprime(a, b):
     
    # If a & b are already non-coprimes
    if (gcd(a, b) != 1):
        return 0
 
    # If a & b can become non-coprimes
    # by only incrementing/decrementing
    # a number only once
    if (gcd(a - 1, b) != 1 or
        gcd(a + 1, b) != 1 or
        gcd(b - 1, a) != 1 or
        gcd(b + 1, a) != 1):
        return 1
 
    # Otherwise
    return 2
 
# Driver Code
if __name__ == '__main__':
     
    A = 7
    B = 17
     
    print(makeCoprime(A, B))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate gcd
static int __gcd(int a, int b)
{
     
    // Everything divides 0
    if (a == 0)
        return b;
    if (b == 0)
        return a;
     
    // Base case
    if (a == b)
        return a;
     
    // a is greater
    if (a > b)
        return __gcd(a - b, b);
         
    return __gcd(a, b - a);
}
 
// Function to find the minimum number of
// increments/decrements operations required
// to make both the numbers non-coprime
static int makeCoprime(int a, int b)
{
     
    // If a & b are already non-coprimes
    if (__gcd(a, b) != 1)
        return 0;
     
    // If a & b can become non-coprimes
    // by only incrementing/decrementing
    // a number only once
    if (__gcd(a - 1, b) != 1 ||
        __gcd(a + 1, b) != 1 ||
        __gcd(b - 1, a) != 1 ||
        __gcd(b + 1, a) != 1)
        return 1;
     
    // Otherwise
    return 2;
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 7, B = 17;
     
    Console.Write(makeCoprime(A, B));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
2

时间复杂度: O(log(A, B))
辅助空间: O(1)