📜  检查给定的一对数字是否已订婚

📅  最后修改于: 2021-04-29 13:33:50             🧑  作者: Mango

给定两个正数NM ,任务是检查给定的数字对(N,M)是否形成了订婚的数字。
例子:

方法:

  1. 求出给定数字N和M的适当除数的总和。
  2. 如果N适当的除数的总和等于M + 1M的适当的除数的总和是等于N + 1,则给定的对形成婚约数。
  3. 否则它就不会形成一对订婚号码。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check whether N is
// Perfect Square or not
bool isPerfectSquare(int N)
{
 
    // Find sqrt
    double sr = sqrt(N);
 
    return (sr - floor(sr)) == 0;
}
 
// Function to check whether the given
// pairs of numbers is Betrothed Numbers
// or not
void BetrothedNumbers(int n, int m)
{
    int Sum1 = 1;
    int Sum2 = 1;
 
    // For finding the sum of all the
    // divisors of first number n
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            Sum1 += i
                    + (isPerfectSquare(n)
                           ? 0
                           : n / i);
        }
    }
 
    // For finding the sum of all the
    // divisors of second number m
    for (int i = 2; i <= sqrt(m); i++) {
        if (m % i == 0) {
            Sum2 += i
                    + (isPerfectSquare(m)
                           ? 0
                           : m / i);
        }
    }
 
    if ((n + 1 == Sum2)
        && (m + 1 == Sum1)) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
}
 
// Driver Code
int main()
{
    int N = 9504;
    int M = 20734;
 
    // Function Call
    BetrothedNumbers(N, M);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Function to check whether N is
// Perfect Square or not
static boolean isPerfectSquare(int N)
{
  
    // Find sqrt
    double sr = Math.sqrt(N);
  
    return (sr - Math.floor(sr)) == 0;
}
  
// Function to check whether the given
// pairs of numbers is Betrothed Numbers
// or not
static void BetrothedNumbers(int n, int m)
{
    int Sum1 = 1;
    int Sum2 = 1;
  
    // For finding the sum of all the
    // divisors of first number n
    for (int i = 2; i <= Math.sqrt(n); i++) {
        if (n % i == 0) {
            Sum1 += i
                    + (isPerfectSquare(n)
                           ? 0
                           : n / i);
        }
    }
  
    // For finding the sum of all the
    // divisors of second number m
    for (int i = 2; i <= Math.sqrt(m); i++) {
        if (m % i == 0) {
            Sum2 += i
                    + (isPerfectSquare(m)
                           ? 0
                           : m / i);
        }
    }
  
    if ((n + 1 == Sum2)
        && (m + 1 == Sum1)) {
        System.out.print("YES" +"\n");
    }
    else {
        System.out.print("NO" +"\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 9504;
    int M = 20734;
  
    // Function Call
    BetrothedNumbers(N, M);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
from math import sqrt,floor
 
# Function to check whether N is
# Perfect Square or not
def isPerfectSquare(N):
    # Find sqrt
    sr = sqrt(N)
 
    return (sr - floor(sr)) == 0
 
# Function to check whether the given
# pairs of numbers is Betrothed Numbers
# or not
def BetrothedNumbers(n,m):
    Sum1 = 1
    Sum2 = 1
 
    # For finding the sum of all the
    # divisors of first number n
    for i in range(2,int(sqrt(n))+1,1):
        if (n % i == 0):
            if (isPerfectSquare(n)):
                Sum1 += i
            else:
                Sum1 += i + n/i
 
    # For finding the sum of all the
    # divisors of second number m
    for i in range(2,int(sqrt(m))+1,1):
        if (m % i == 0):
            if (isPerfectSquare(m)):
                Sum2 += i
            else:
                Sum2 += i + (m / i)
 
    if ((n + 1 == Sum2) and (m + 1 == Sum1)):
        print("YES")   
    else:
        print("NO")
 
# Driver Code
if __name__ == '__main__':
    N = 9504
    M = 20734
 
    # Function Call
    BetrothedNumbers(N, M)
 
# This code is contributed by Surendra_Gangwar


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check whether N is
// perfect square or not
static bool isPerfectSquare(int N)
{
 
    // Find sqrt
    double sr = Math.Sqrt(N);
 
    return (sr - Math.Floor(sr)) == 0;
}
 
// Function to check whether the given
// pairs of numbers is Betrothed numbers
// or not
static void BetrothedNumbers(int n, int m)
{
    int Sum1 = 1;
    int Sum2 = 1;
 
    // For finding the sum of all the
    // divisors of first number n
    for(int i = 2; i <= Math.Sqrt(n); i++)
    {
       if (n % i == 0)
       {
           Sum1 += i + (isPerfectSquare(n) ?
                                 0 : n / i);
       }
    }
 
    // For finding the sum of all the
    // divisors of second number m
    for(int i = 2; i <= Math.Sqrt(m); i++)
    {
       if (m % i == 0)
       {
           Sum2 += i + (isPerfectSquare(m) ?
                                 0 : m / i);
       }
    }
 
    if ((n + 1 == Sum2) && (m + 1 == Sum1))
    {
        Console.Write("YES" + "\n");
    }
    else
    {
        Console.Write("NO" + "\n");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 9504;
    int M = 20734;
 
    // Function Call
    BetrothedNumbers(N, M);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
NO

时间复杂度: O(√N+√M)