📌  相关文章
📜  检查两个数的除数之和是否相同

📅  最后修改于: 2021-04-29 03:53:48             🧑  作者: Mango

给定两个数字n1和n2,我们需要检查这些数字是否相等。
等效数字是指其适当除数之和相同的数字。
例如,159、559和703是等效数字。这是因为所有三个数字的适当除数之和为57。

例子 :

找到给定数字的“完美数”中所实现的适当除数之和,然后检查两个和是否相等。

C++
// C++ program to find if two numbers are
// equivalent or not
#include 
using namespace std;
  
// Function to calculate sum of all proper divisors
// num --> given natural number
int divSum(int n)
{
    // To store sum of divisors
    long long int sum = 1;
  
    // Find all divisors and add them
    for (long long int i = 2; i * i <= n; i++)
        if (n % i == 0)
            sum = sum + i + n / i;
  
    return sum;
}
  
// Function to check if both numbers
// are equivalent or not
bool areEquivalent(int num1, int num2)
{
    return divSum(num1) == divSum(num2);
}
  
// Drivers code
int main()
{
    int num1 = 559, num2 = 703;
  
    areEquivalent(num1, num2) ? 
                  cout << "Equivalent" : 
                  cout << "Not Equivalent";
  
    return 0;
}


Java
// Java program to find if two numbers are
// equivalent or not
import java.math.*;
  
class GFG {
  
    // Function to calculate sum of all proper
    // divisors num --> given natural number
    static int divSum(int n)
    {
        // To store sum of divisors
        int sum = 1;
  
        // Find all divisors and add them
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                sum = sum + i + n / i;
  
        return sum;
    }
  
    // Function to check if both numbers
    // are equivalent or not
    static boolean areEquivalent(int num1, int num2)
    {
  
        return divSum(num1) == divSum(num2);
    }
  
    // Drivers code
    public static void main(String[] args)
    {
        int num1 = 559;
        int num2 = 703;
  
        if (areEquivalent(num1, num2))
            System.out.println("Equivalent");
        else
            System.out.println("Not Equivalent");
    }
}


Python3
# Python3 program to find 
# if two numbers are
# equivalent or not
import math
  
# Function to calculate sum 
# of all proper divisors
# num --> given natural number
def divSum(n):
      
    # To store sum of divisors
    sum = 1;
  
    # Find all divisors
    # and add them
    i = 2;
    while(i * i <= n):
        if (n % i == 0):
            sum = (sum + i +
                   math.floor(n / i));
        i += 1;
  
    return sum;
  
# Function to check 
# if both numbers
# are equivalent or not
def areEquivalent(num1, num2):
    return divSum(num1) == divSum(num2);
  
# Driver code
num1 = 559; 
num2 = 703;
  
if (areEquivalent(num1, num2) == True):
    print("Equivalent");
else:
    print("Not Equivalent");
              
# This code is contributed by mits


C#
// C# program to find if two 
// numbers are equivalent or not
using System;
  
class GFG
{
      
    // Function to calculate sum 
    // of all proper divisors 
    // num --> given natural number
    static int divSum(int n)
    {
        // To store sum of divisors
        int sum = 1;
  
        // Find all divisors
        // and add them
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0)
                sum = sum + i + n / i;
  
        return sum;
    }
  
    // Function to check if 
    // both numbers are 
    // equivalent or not
    static bool areEquivalent(int num1, 
                              int num2)
    {
        return divSum(num1) == divSum(num2);
    }
  
    // Driver code
    static public void Main ()
    {
        int num1 = 559;
        int num2 = 703;
  
        if (areEquivalent(num1, num2))
            Console.WriteLine("Equivalent");
        else
            Console.WriteLine("Not Equivalent");
    }
}
  
// This code is contributed by m_kit


PHP
 given natural number
function divSum($n)
{
    // To store sum of divisors
    $sum = 1;
  
    // Find all divisors
    // and add them
    for ($i = 2; $i * $i <= $n; $i++)
        if ($n % $i == 0)
            $sum = $sum + $i + 
                   floor($n / $i);
  
    return $sum;
}
  
// Function to check 
// if both numbers
// are equivalent or not
function areEquivalent($num1, $num2)
{
    return divSum($num1) == divSum($num2);
}
  
// Driver code
$num1 = 559; $num2 = 703;
  
if (areEquivalent($num1, $num2) == true) 
    echo "Equivalent" ;
              
else
    echo "Not Equivalent";
              
// This code is contributed by ajit
?>


输出:
Equivalent