Program to Check if Two Numbers are a Friendly Pair in Python
Friendly Pair
A friendly pair (also called an amicable pair) is a pair of numbers where the sum of their proper divisors divided by the number itself results in the same value for both numbers. For example, (220, 284) is a friendly pair.
We will explore a method to check if a given pair of numbers is a friendly pair using Python programming.
Method: Using a Function
We calculate the sum of proper divisors for both numbers and check if their ratios match.
def sum_of_divisors(num): return sum(i for i in range(1, num // 2 + 1) if num % i == 0) # Get user input num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) sum1 = sum_of_divisors(num1) sum2 = sum_of_divisors(num2) if sum1 / num1 == sum2 / num2: print(f"{num1} and {num2} are a Friendly Pair") else: print(f"{num1} and {num2} are not a Friendly Pair")
Output:
Enter first number: 220 Enter second number: 284 220 and 284 are a Friendly Pair