Comprobar si la suma de los divisores de dos números es la misma

Dados dos números n1 y n2, debemos verificar si estos números son números equivalentes o no. 
Los números equivalentes son números tales que las sumas de sus divisores propios son iguales. 
Por ejemplo, 159, 559 y 703 son números equivalentes. Esto se debe a que los tres números tienen 57 como la suma de sus divisores propios. 

Ejemplos:  

Entrada: n1 = 559, n2 = 703 
Salida: Sí. 
Explicación: Ambos números tienen 57 como suma de sus divisores propios. 
  
Entrada: n1 = 36, n2 = 57 
Salida: No. 
Explicación: 36 tiene una suma de 55 mientras que 57 tiene una suma de 23 de sus divisores propios. 

Encuentre la suma de los divisores propios como se implementa en Número perfecto para los números dados y luego verificará si ambas sumas son iguales o no. 
 

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>
Producción: 

Equivalent

 

Publicación traducida automáticamente

Artículo escrito por Prasad_Kshirsagar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *