Skip to content
Related Articles

Related Articles

Improve Article

Check if sum of divisors of two numbers are same

  • Last Updated : 05 May, 2021

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




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

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




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

Javascript




<script>
    // Javascript 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
        let sum = 1;
   
        // Find all divisors
        // and add them
        for (let i = 2; i * i <= n; i++)
            if (n % i == 0)
                sum = sum + i + parseInt(n / i, 10);
   
        return sum;
    }
   
    // Function to check if
    // both numbers are
    // equivalent or not
    function areEquivalent(num1, num2)
    {
        return divSum(num1) == divSum(num2);
    }
     
    let num1 = 559;
    let num2 = 703;
 
    if (areEquivalent(num1, num2))
      document.write("Equivalent");
    else
      document.write("Not Equivalent");
 
</script>
Output: 
Equivalent

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :