Check if sum of divisors of two numbers are same

Given two numbers n1 & n2, We need to check whether these numbers are equivalent numbers or not.
Equivalent numbers are numbers such that the sums of their proper divisors are the same.
For example, 159, 559, and 703 are equivalent numbers. This is because all the three numbers have 57 as the sum of their proper divisors.

Examples :

Input : n1 = 559, n2 = 703
Output : Yes.
Explanation: Both numbers have 57 as a sum of their proper divisors.
 
Input : n1 = 36, n2 = 57
Output : No.
Explanation: 36 has sum 55 while 57 has sum 23 of their proper divisors.



Find the sum of proper divisors as implemented in for Perfect number for the given numbers and then will check if both sums are equal or not.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if two numbers are
// equivalent or not
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find 
// if two numbers are
// equivalent or not
  
// Function to calculate sum 
// of all proper divisors
// num --> 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
?>

chevron_right


Output:

Equivalent


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.