Check Friendly Pair

Check whether two numbers form a friendly pair (same abundancy index).

PythonIntermediate
Python
# 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

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.