📌  相关文章
📜  检查友好对

📅  最后修改于: 2021-04-24 16:53:47             🧑  作者: Mango

可和数字是两个不同的数字,因此彼此相关,以使每个数字的适当除数之和等于另一个数字。 (数字的适当除数是该数字的正数,而不是数字本身。
例子:

Input : x = 220, y = 284
Output : Yes 
Proper divisors of 220 are 1, 2, 4, 5,
10, 11, 20, 22, 44, 55 and 110. Sum of 
these is 284. Proper divisors of 284 
are 1, 2, 4, 71 and 142 with sum 220.

Input : 1 2
Output :No

逻辑很简单。我们将两个数字的适当除数之和进行比较,然后将一个数字与另一个数字的和进行比较。

C++
// CPP program to check if two numbers are
// Amicable or not.
#include 
using namespace std;
  
// Function to calculate sum of all 
// proper divisors of a given number
int divSum(int n)
{
    // Sum of divisors
    int result = 0;
    
    // find all divisors which divides 'num'
    for (int i = 2; i <= sqrt(n); i++)
    {
        // if 'i' is divisor of 'n'
        if (n % i == 0)
        {
            // if both divisors are same
            // then add it once else add
            // both
            if (i == (n / i))
                result += i;
            else
                result += (i + n/i);
        }
    }
    
    // Add 1 and n to result as above loop
    // considers proper divisors greater 
    // than 1.
    return (result + 1);
}
    
// Returns true if x and y are Amicable
// else false.
bool areAmicable(int x, int y)
{
    if (divSum(x) != y)
       return false;
  
    return (divSum(y) == x);
}
  
int main() {
    int x = 220, y = 284;
    if (areAmicable(x, y))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}


Java
// JAVA program to check if two numbers are
// Amicable or not.
import java.io.*;
  
class GFG 
{
    // Function to calculate sum of all 
    // proper divisors of a given number
    static int divSum(int n)
    {
        // Sum of divisors
        int result = 0;
      
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
      
        // Add 1 and n to result as above loop
        // considers proper divisors greater 
        // than 1.
        return (result + 1);
    }
      
    // Returns true if x and y are Amicable
    // else false.
    static boolean areAmicable(int x, int y)
    {
        if (divSum(x) != y)
        return false;
      
        return (divSum(y) == x);
    }
      
    public static void main (String[] args) 
    {
        int x = 220, y = 284;
        if (areAmicable(x, y))
        System.out.println( "Yes");
        else
        System.out.println("No");
      
    }
} 
  
// This code is contributed by vt_m.


Python3
# Python program to check 
# if two numbers are
# Amicable or not.
import math
  
# def to calculate sum 
# of all proper divisors
# of a given number
def divSum(n) :
      
    # Sum of divisors
    result = 0
  
    # find all divisors 
    # which divides 'num'
    for i in range(2, int(math.sqrt(n)) + 1) :
          
        # if 'i' is 
        # divisor of 'n'
        if (n % i == 0) :
              
            # if both divisors are same
            # then add it once else add
            # both
            if (i == int(n / i)) :
                result = result + i
            else :
                result = result + 
                         (i + int(n / i))
  
    # Add 1 and n to result 
    # as above loop considers
    # proper divisors greater 
    # than 1.
    return (result + 1)
  
# Returns true if x and y 
# are Amicable else false.
def areAmicable(x, y) :
  
    if (divSum(x) != y) :
        return False
          
    return (divSum(y) == x) 
  
# Driver Code
x = 220
y = 284
if (areAmicable(x, y)) :
    print ("Yes")
else :
    print ("No")
      
# This code is contributed by 
# Manish Shaw(manishshaw1)


C#
// C# program to check if two numbers are
// Amicable or not.
using System;
  
class GFG 
{
    // Function to calculate sum of all 
    // proper divisors of a given number
    static int divSum(int n)
    {
        // Sum of divisors
        int result = 0;
      
        // find all divisors which divides 'num'
        for (int i = 2; i <= Math.Sqrt(n); i++)
        {
            // if 'i' is divisor of 'n'
            if (n % i == 0)
            {
                // if both divisors are same
                // then add it once else add
                // both
                if (i == (n / i))
                    result += i;
                else
                    result += (i + n / i);
            }
        }
      
        // Add 1 and n to result as above loop
        // considers proper divisors greater 
        // than 1.
        return (result + 1);
    }
      
    // Returns true if x and y are Amicable
    // else false.
    static bool areAmicable(int x, int y)
    {
        if (divSum(x) != y)
        return false;
      
        return (divSum(y) == x);
    }
      
    public static void Main () 
    {
        int x = 220, y = 284;
          
        if (areAmicable(x, y))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine("No");
      
    }
} 
  
// This code is contributed by vt_m.


PHP


Javascript


输出:

Yes