Check Friendly Pair
Check whether two numbers form a friendly pair (same abundancy index).
IntermediateTopic: Conditional Programs
Python Check Friendly Pair Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to check friendly pair
def abundancy_index(n: int) -> float:
total = 0
for i in range(1, n + 1):
if n % i == 0:
total += i
return total / n
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a <= 0 or b <= 0:
print("Numbers must be positive.")
else:
if abundancy_index(a) == abundancy_index(b):
print(a, "and", b, "form a friendly pair")
else:
print(a, "and", b, "do not form a friendly pair")Output
Enter first number: 6 Enter second number: 28 6 and 28 form a friendly pair
Understanding Check Friendly Pair
Two numbers are friendly if the ratio (sum of divisors / number) is equal for both.
We implement a helper function to compute the abundancy index and compare for the two inputs.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.