Python program to check whether two numbers are Friendly Pair

Check Whether or Not the Two Numbers  are Friendly Pairs in Python

Given two integer numbers as the input, the objective is to check whether or not the two numbers are Friendly pairs of each other. Therefore, we’ll write a Program to Check Whether or Not the Two Numbers are Friendly Pairs in Python.

Example
Input : 6 28
Output : Yes, they are a friendly pair
friendly pair or not in python

Check Whether or Not the Two Numbers are Friendly Pairs in Python Language

Given two integer inputs as the numbers, the objective is to check whether the (divisors)/number ratio for the both the numbers are same or not. To do so we’ll run a loop to first find all the factors the the two numbers and then we’ll get the ratio using the above mentioned formula. We check whether the ratios are matching or not. Here are some of the methods we’ll use to Check Whether or Not the Two Numbers are Friendly Pairs in Python Language,

  • Method 1: Using the Range until Number.
  • Method 2: Using the Range until Sqrt( Number )

We’ll discuss the above mentioned methods in detail in the upcoming sections. Do check the blue box mentioned below for better understanding. Check out the python page to Find all the Factors of a Number for better understanding.

Method 1: Using the Range until Number

In this method we’ll check for factors in the range of 2 to the number itself. We’ll define a function that returns all the factors of a given number. Then we’ll sum them up and divide with the number itself to get the required ratio. In the end we’ll do the same for both the numbers and match their ratio.

Python Code

Run
def printDivisors(n, factors) :
    i = 1
    while i <= n :
        if (n % i==0) :
            factors.append(i)
        i = i + 1
    return sum(factors) - n

if __name__ == "__main__": 
  number1, number2 = 6, 28
  if int(printDivisors(number1, [])/number1) == int(printDivisors(number2, [])/number2):
    print("Friendly pair")
  else:
    print("Not a Friendly Pair")

Output

Friendly pair

Method 2: Using the Range until Sqrt( Number )

In this method we’ll use a loop within a function to find all the factors of the given number input. We’ll run the loop from 2 to the square root of the number. Once we get all the factors except the number itself, we’ll sum them all up and divide it with the same number. We’ll do the same for both numbers and match the ratio.

Python Code

Run
def printDivisors(n, factors) :
     
    # Note that this loop runs till square root
    i = 1
    while i <= pow(n,0.5):
         
        if (n % i == 0) :
             
            # If divisors are equal, print only one
            if (n / i == i) :
                factors.append(i)
            else :
                # Otherwise print both
                factors.append(i)
                factors.append(int(n/i))
        i = i + 1
    return sum(factors) - n

if __name__ == "__main__": 
  number1, number2 = 6, 28
  if int(printDivisors(number1, [])/number1) == int(printDivisors(number2, [])/number2):
    print("Friendly pair")
  else:
    print("Not a Friendly Pair")

Output

Friendly Pair

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

26 comments on “Python program to check whether two numbers are Friendly Pair”


  • Parvez

    n = int(input(‘Enter your first number’))
    m = int(input(‘Enter your second number’))

    nSum = 0
    for i in range(1, n+1):
    if n % i == 0:
    nSum += i

    mSum = 0
    for j in range(1, m+1):
    if m % j == 0:
    mSum += j

    if nSum / n == mSum / m:
    print(‘Yes {}, {} is a Friendly Pair.’.format(n, m))
    else:
    print(‘Yes {}, {} is not a Friendly Pair.’.format(n, m))


  • SOUMAJIT

    a=int(input())
    b=int(input())
    h=0
    for i in range(2,(a+1)):
    if(a%i==0 and b%i==0):
    h=1
    if(h==0):
    print(“No”, a , “,” , b ,”are not friendly Pair”)
    else:
    print(“Yes” , a , “,” , b ,”are friendly Pair”)


  • Sonal

    num1=int(input(“Enter the first number “))
    num2=int(input(“Enter the second number “))
    sum1,sum2=0,0
    for i in range(1,num1+1):
    if num1%i==0:
    sum1+=i
    else:
    continue
    for i in range(1,num2+1):
    if num2%i==0:
    sum2+=i
    else:
    continue
    if sum1/num1==sum2/num2:
    print(“({},{}) is a friendly pair”.format(num1,num2))
    else:
    print(“({},{}) is not a friendly pair”.format(num1,num2))


  • Sonal

    num1=int(input(“Enter the first number “))
    num2=int(input(“Enter the second number “))
    sum1,sum2=0,0
    for i in range(1,num1):
    if num1%i==0:
    sum1+=i
    else:
    continue
    for i in range(1,num2):
    if num2%i==0:
    sum2+=i
    else:
    continue
    if sum1/num1==sum2/num2:
    print(“({},{}) is a friendly pair”.format(num1,num2))
    else:
    print(“({},{}) is not a friendly pair”.format(num1,num2))


  • kdhote99

    I wrote this code for friendly numbers is this correct? n1=int(input(“Enter no. for its factors :- “))
    n2=int(input(“Enter no. for its factors :- “))
    sum1=0;sum2=0
    for i in range (1,n1+1):
    if n1%i==0:
    sum1=sum1+i
    for i in range (1,n2+1):
    if n2%i==0:
    sum2=sum2+i
    if (sum1/n1==sum2/n2):
    print(“{} and {} are friendly pairs and their Abundancy index is {}”.format(n1,n2,sum1/n1))
    else:
    print(“{} and {} are not friendly pairs”.format(n1,n2))


  • kdhote99

    I wrote this code for friendly numbers is this correct?

    n1=int(input(“Enter no. for its factors :- “))
    n2=int(input(“Enter no. for its factors :- “))
    sum1=0;sum2=0
    for i in range (1,n1+1):
    if n1%i==0:
    sum1=sum1+i
    for i in range (1,n2+1):
    if n2%i==0:
    sum2=sum2+i
    if (sum1/n1==sum2/n2):
    print(“{} and {} are friendly pairs and their Abundancy index is {}”.format(n1,n2,sum1/n1))
    else:
    print(“{} and {} are not friendly pairs”.format(n1,n2))


  • Shubhanjay

    num1 = int(input(“Enter a number”))
    num2 = int(input(“Enter a number”))

    def sumofdivisor(num):
    sum = 0
    for i in range(1, num):
    if num % i == 0:
    sum = sum + i
    return sum

    sumofnum1 = sumofdivisor(num1) + num1
    sumofnum2 = sumofdivisor(num2) + num2
    print(sumofnum1)
    print(sumofnum2)
    if sumofnum1 / num1 == sumofnum2 / num2:
    print(“yes”, num1, ” and “, num2, ” are friendly pair”)
    else:
    print(“No”, num1, ” and “, num2, ” are not a friendly pair”)


  • KIRA

    def abundant_number(num):
    count = 0
    num = int(num)
    for i in range(1, num+1):
    if num % i == 0:
    count += i

    a = input(“A number : “)
    b = input(“A number : “)
    x = abundant_number(a)
    y = abundant_number(b)

    if x == y:
    print(“{} and {} are Friendly Pair”.format(a, b))
    else:
    print(“Not Friendly”)


  • Sriram

    def abundancy(number):
    sum_of_divisors = 0
    for i in range(1, number+1):
    if number%i == 0:
    sum_of_divisors+=i
    return sum_of_divisors
    def friendly(number1, number2):
    if abundancy(number1)/number1 == abundancy(number2)/number2:
    print(f”{number1} and {number2} are friendly pair”)
    else:
    print(f”{number1} and {number2} are not friendly pair”)
    friendly(6, 28)


  • DHANANJAY

    ———————————————–
    n = int(input(“Enter first no: “))
    m = int(input(“Enter second no: “))
    sum1 = 0
    sum2 = 0
    for i in range(1,n):
    if(n%i==0):
    sum1 += i
    for j in range(1,m):
    if(m%j==0):
    sum2 += j
    if(n/sum1 == m/sum2):
    print(“{} and {} is a friendly pair”.format(n,m))
    else:
    print(“{} and {} is not a friendly pair”.format(n,m))


  • Giri

    a=int(input(“enter the number:”))
    b=int(input(“enter the number:”))
    e=0
    f=0
    for i in range(1,a+1):
    if(a%i==0):
    e=e+i
    print(e)
    for j in range(1,b+1):
    if(b%j==0):
    f=f+j
    print(f)
    if(e%a==0 and f%b==0):
    print(“yes”)
    else:
    print(“no”)


    • Giri

      a=int(input(“enter the number:”))
      b=int(input(“enter the number:”))
      e=0
      f=0
      h=0
      o=0
      for i in range(1,a+1):
      if(a%i==0):
      e=e+i
      h=e//a
      print(h)
      for j in range(1,b+1):
      if(b%j==0):
      f=f+j
      o=f//b
      print(o)

      if(h==o):
      print(“yes”)
      else:
      print(“no”)


  • Akash

    n1=int(input(“enter”))
    n2=int(input(“enter”))
    l2=[]
    l1=[n1,n2]
    for i in l1:
    sum=0
    for j in range(1,i+1):
    if i%j==0:
    sum=sum+j
    l2.append(sum)
    if l2[0]//l1[0] == l2[1]//l1[1]:
    print(“Friendly Pair”)
    else:
    print(“Not Friendly Pair”)