Check for Amicable Pair


The concept of an amicable pair of numbers or a friendly pair of numbers seems interesting right? So what exactly are amicable pairs of numbers?

Two numbers are said to be an amicable pair of numbers only if the sum of the first number's proper divisors equals the sum of the second number's proper divisors.

Also just in case you had forgotten, The Pythagoreans were always known for associating numbers with characteristics like justice and friendship.

Problem Statement

Implement a program to check whether the given pair of numbers is an amicable pair or not.

Approach

We first consider the proper divisors of the given pair of numbers. Add the proper divisors of the first number and obtain the sum. Similarly, add the proper divisors of the second number and obtain the sum.

If the sum of the proper divisors of the first number equals the sum of the proper divisors of the second number. We call the pair of numbers an amicable pair. Otherwise, if the sum of the proper divisors of the first number does not equal the sum of the proper divisors of the second number. Then the pair of numbers is not an amicable pair of numbers.

Sample Input Outputs

Input

num1 = 220, num2 = 284

Output

220 and 280 are amicable pair

Explanation

Consider the numbers 220 and 284.

The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110. Adding all these divisors we get the sum as 284. Now take the proper divisors of 284. The proper divisors are 1, 2, 4, 71, and 142. Adding all these divisors we get the sum as 284. Since both the sum we obtained is equal we can say that the pair of numbers

220 and 280 are amicable pairs.

Input

num1 = 220, num2 = 280

Output

220 and 280 are not amicable pair

Explanation

Consider the numbers 220 and 280.

The proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110. Adding all these divisors we get the sum as 284. Now take the proper divisors of 284. The proper divisors are 1, 2, 4, 5, 7, 8, 10, 14, 20, 28, 35, 40, 56, 70 and 140.

Adding all these divisors we get the sum as 440. Since both the sum we obtained in this case is not equal we can say that the pair of numbers 220 and 280 are not amicable pairs.

Algorithm

Step 1: Input the numbers.

Step 2: Find the sum of all divisors for both numbers.

Step 3: Finally check if the sum of the divisors of one number is equal to the other number or not.

Step 4: If yes, it is an Amicable number and otherwise not.

Below is a C program to check whether the given pair of numbers is an amicable pair or not is given below.

Example

#include<stdio.h>
int main(){
   int i,n1=220,n2=284,DivSum1=0,DivSum2=0;
   //Use one for loop and check for each number starting from 1 to n1 -1.
   for(int i=1;i<n1;i++){
    //Check for each number in the loop if it can divide the number firstNumber or not. If yes, add this number to the DivSum1. After the loop completes, DivSum1 includes the sum of all the divisors for firstNumber.
      if(n1 % i == 0){
         DivSum1 = DivSum1 + i;
      }
   }
   //Same way, determine the sum of all divisors for the second number and save it in DiviSum2.
   for(int i=1;i<n2;i++){
      if(n2 % i == 0){
         DivSum2= DivSum2+ i;
      }
   }
   //Last, check if the sum of divisors of the first number is equal to the second number or not. If it is equal, then print that the numbers are Amicable numbers. Otherwise the numbers are not amicable pairs.
   if((n1== DivSum2) && (n2 == DivSum1)){
      printf("%d and %d are Amicable numbers\n",n1,n2);
   }else{
      printf("%d and %d are not Amicable numbers\n",n1,n2);
   }
}

Output

On execution, it will produce the following output:

220 and 284 are Amicable numbers.

Conclusion

Likewise, we can determine whether the given pair of numbers is an amicable pair or not by inputting any pair of numbers. The challenge of determining whether the given pair of numbers is an amicable pair or not is resolved in this article. Here C programming codes to check the given numbers form an amicable pair or not is provided.

Updated on: 23-Aug-2023

879 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements