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.
Let's try and understand it better using an example
Example Input : 6 28 Output : Yes, they are a friendly pair Explanation : The factors of 6 and 28 except the numbers themselves are 1, 2, 3 and 1, 2, 4, 7, 14 respectively. Now the sum of factors of both the numbers are 6 and 28 respectively. When we divide the sums with the numbers we get 1 and 1 respectively. As the ratio of both the number match, they are considered as a friendly pair.Therefore, from the above mentioned definition and example, we'll check for friendly pairs.
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
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
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
Login/Signup to comment
def friendly(n):
s=0
for i in range(1,int(n/2)+1):
if n%i==0:
s=s+i
return s
a,b=input().split()
a,b=int(a),int(b)
a1=friendly(a)
a2=friendly(b)
if a1//a==a2//b:
print(“f”)
else:
print(“n”)
Hey, Our mentors can help you with your technical queries on Discord
# Python program to check whether two numbers are Friendly Pair.
#Example
#Input : 6 28
#Output : Yes, they are a friendly pair
#Explanation : The factors of 6 and 28 except the numbers themselves are 1, 2, 3 and 1, 2, 4, 7, 14 respectively.
#Now the sum of factors of both the numbers are 6 and 28 respectively.
#When we divide the sums with the numbers we get 1 and 1 respectively.
#As the ratio of both the number match, they are considered as a friendly pair.
n1=int(input())
n2=int(input())
sum=0
sum1=0
for i in range(1,n1):
if n1%i==0:
sum=sum+i
for j in range(1,n2):
if n2%j==0:
sum1=sum1+j
if sum//n1 == sum1//n2:
print(“Friendly pair”)
else:
print(“Not Friendly pair”)
# Try This
def factors_sum(n):
s=0
for i in range(1,n//2+1):
if n%i==0:
s+=i
return s
a,b=input().split()
a,b=int(a),int(b)
sum_a=factors_sum(a)
sum_b=factors_sum(b)
print(“\nFriendly Pair” if a/sum_a==b/sum_b else “Not a Frendly Pair”)
a=int(input(“”))
b=int(input(“”))
s=0
d=0
for i in range(1,a):
if(a%i==0):
s=s+i
for i in range(1,b):
if(b%i==0):
d=d+i
if(a/s==b/d):
print(“yes”)
else:
print(“no”)
num1=6
num2=28
sum1=0
sum2=0
for i in range(1,num1):
if(num1%i==0):
sum1=sum1+i
for i in range(1,num2):
if(num2%i==0):
sum2=sum2+i
if (sum1==num1 and sum2==num2):
print(” friendly pair”)
else:
print(“not friendly pair”)
N1, N2 = 6, 28
ls1, ls2 = [i for i in range(1, N1) if N1 % i == 0], [i for i in range(1, N2) if N2 % i == 0]
print(“FriendlyPair” if sum(ls1)/N1==1 and sum(ls2)/N2==1 else “False”)
n,m=int(input()),int(input())
s,c=0,0
for i in range(1,n):
if n%i==0:
s+=i
for j in range(1,m):
if m%j==0:
c+=j
if (s/n)==(c/m):
print(“friendly pair”)
else:
print(“not friendly pair”)
a,b=[int(x) for x in input().split()]
c,d= sum([i for i in range(1,a) if a%i==0]),sum([i for i in range(1,b) if b%i==0])
if(c//a==1 and d//b==1):
print(“friendly pair”)
else:
print(“not a friendly pair”)
n , m = map(int,input(“Give input = “).split())
def allfactors(n):
factors = [1]
for i in range(2, (n // 4) + 2):
if n % i == 0 and i not in factors:
factors.append(i)
if (n // i) not in factors:
factors.append(n // i)
return factors
def isFriendlyPair(num1,num2):
sum1 = sum(allfactors(num1))
sum2 = sum(allfactors(num2))
if sum1/num1 == sum2/num2:
return “Yes this number is a Friendly Pair”
else:
return “No it is not a Friendly Pair”
print(isFriendlyPair(n,m))
n1=int(input())
n2=int(input(” “))
sum1=0
sum2=0
for i in range(1,n1):
if n1%i==0:
sum1+=i
print(sum1)
for j in range(1,n2):
if n2%j==0:
sum2+=j
print(sum2)
a=sum1/n1
b=sum2/n2
print(a,b)
if a==b:
print(“friendly pairs”)
else:
print(“no”)
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”)
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”)