Check for Amicable Pair - GeeksforGeeks

24 Mar, 2021
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. Examples:  
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
 
The logic is very simple. We compare sum of the proper divisors of both numbers and compare sum for one number with other number. 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to check if two numbers are// Amicable or not.#include <bits/stdc++.h>using namespace std;  // Function to calculate sum of all // proper divisors of a given numberint 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 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.
# Python program to check # if two numbers are# Amicable or not.import math  # def to calculate sum # of all proper divisors# of a given numberdef 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 Codex = 220y = 284if (areAmicable(x, y)) :    print ("Yes")else :    print ("No")      # This code is contributed by # Manish Shaw(manishshaw1)
// 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// PHP program to check if two// numbers are Amicable or not.  // Function to calculate sum of all // proper divisors of a given numberfunction divSum( $n){          // Sum of divisors    $result = 0;      // find all divisors     // which divides 'num'    for ($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.function areAmicable($x, $y){    if (divSum($x) != $y)        return false;              return (divSum($y) == $x);}      // Driver Code    $x = 220;    $y = 284;    if (areAmicable($x, $y))        echo "Yes";    else        echo "No";  // This code is contributed by anuj_67.?>
<script>    // Javascript program to check if two // numbers are Amicable or not.   // Function to calculate sum of all // proper divisors of a given number function divSum(n) {           // Sum of divisors     let result = 0;       // find all divisors     // which divides 'num'     for (let 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. function areAmicable(x, y) {     if (divSum(x) != y)         return false;               return (divSum(y) == x); }       // Driver Code     let x = 220;     let y = 284;     if (areAmicable(x, y))         document.write("Yes");     else        document.write("No");   // This code is contributed by _saurabh_jaiswal.   </script>
Output: 
Yes
 
YouTubeGeeksforGeeks507K subscribersAmicable Numbers | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:52•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=gYX-A6U-XDw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
vt_m
manishshaw1
_saurabh_jaiswal
divisors
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Generate all permutation of a set in Python
Check if a number is Palindrome
Program to multiply two matrices
Segment Tree | Set 1 (Sum of given range)
Merge two sorted arrays with O(1) extra space
How to check if a given point lies inside or outside a polygon?
Count ways to reach the n'th stair