Python program to check whether two numbers are Friendly Pair

Friendly Pair

Here we will discuss how to check whether a pair of numbers is a Friendly Pair or not. In number theory, friendly pairs are two numbers with a common abundancy index, the ratio between the sum of divisors of a number and the number itself i.e ?(n)/n. S o, two number n and m are friendly number if
?(n)/n = ?(m)/m.
where ?(n) is the sum of divisors of n.

Example:

  • n=6 , m=28
    • Divisor of 6 are 1, 2, 3, 6.
      Divisor of 28 are 1, 2, 4, 7, 14, 28. Sum of divisor of 6 and 28 are 12 and 56 respectively. Abundancy index of 6 and 28 are 2. So they are friendly pair.
  • n=18 , m=7
    • They are not friendly pair.
Friendly Pair using Python

Implementation:

  • Find the sum of divisors of n and then of m .
  • Find the abundancy idex of both the number say x and y.
  • If x==y then it is Friendly pair else not .

Python Code:

#’Enter the numbers to check’
n=int(input())
m=int(input())
import math 
sum_n=1 + n  #sum of divisor of n 
sum_m=1 + m  #sum of divisor of m
i=2 
j=2 
#finding divisor
while(i<=math.sqrt(n)):
    if(n%i==0):
        if(n//i==i):
            sum_n+=i 
            
        else:
            sum_n+=i + n//i 
            
    i=i+1 
    
while(j<=math.sqrt(m)):
    if(m%j==0):
        if(m//j==j):
            sum_m+=j
            
        else:
            sum_m+=j + m//j 
            
    j=j+1 
if(sum_n/n==sum_m/m):
    print(‘Yes’ , n , ‘,’ , m ,‘ are friendly Pair’
else:
    print(‘No’, n , ‘,’ , m ,‘ are not friendly Pair’)
Input:

6

28

Output:

Yes 6 , 28 are friendly Pair