PYTHON:Check Friendly Pair

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

IntermediateConditional Programs

Python Program Code

This program helps you to learn the fundamental structure and syntax of Python programming.

check-friendly-pair.py
# 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")
Terminal 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.

Practical Learning Notes for Check Friendly Pair

This Python program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.