# 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
``` ## 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`

### Related Banners

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

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

• AMIT

num1=int(input(“Enter the number: “))
num2=int(input(“Enter the number : “))
def sumfactor(n):
sum=1
for i in range(2,n):
if n%i== 0:
sum = sum + i
return sum

a=sumfactor(num1)
b=sumfactor(num2)

if a/num1 == b/num2 :
print(“It is the friendly pair”)
else:
print(“It is not the friendly pair”) 3
• Ashish

n=int(input(“Enter first Number “))
m=int(input(“Enter Second Number “))
a=0
b=0
for i in range(1,n+1):
if n%i==0:
a=a+i
else:
pass
for i in range(1,m+1):
if m%i==0:
b=b+i
else:
pass
print(a,b)
if a/n==b/m:
print(“Friendly Number”)
else:
print(“Not Friendly Number”) 0