Python Program to Check if Two Numbers Are Amicable Numbers

1. Introduction

In this tutorial, we will learn how to write a Python program to check if two numbers are amicable.

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. The smallest pair of amicable numbers is (220, 284). They are special in number theory and are often studied in the context of divisors and algebraic structures.

A pair of numbers (m, n) is called amicable if the sum of proper divisors (excluding the number itself) of m equals n and the sum of proper divisors of n equals m. Proper divisors of a number include all divisors of the number other than the number itself.

2. Program Steps

1. Define two numbers to check if they are an amicable pair.

2. Write a function to calculate the sum of proper divisors of a number.

3. Use this function to find the sum of divisors for both numbers.

4. Compare the sums to the respective other number in the pair.

5. Print whether the numbers are amicable.

3. Code Program

# Function to calculate the sum of proper divisors of a number
def sum_of_divisors(num):
    sum_div = 0
    for i in range(1, num):
        if num % i == 0:
            sum_div += i
    return sum_div

# Function to check if two numbers are amicable
def are_amicable(num1, num2):
    return sum_of_divisors(num1) == num2 and sum_of_divisors(num2) == num1

# Numbers to be checked
num_a = 220
num_b = 284
# Check if the numbers are amicable and print the result
if are_amicable(num_a, num_b):
    print(f"{num_a} and {num_b} are amicable numbers.")
    print(f"{num_a} and {num_b} are not amicable numbers.")


220 and 284 are amicable numbers.


1. sum_of_divisors is a function that calculates the sum of all proper divisors of num by iterating through numbers from 1 to num - 1 and adding those that are divisors (num % i == 0).

2. are_amicable is a function that takes two numbers, num1, and num2, and checks whether they are amicable by comparing the sum of divisors of num1 to num2 and vice versa.

3. num_a and num_b are set to 220 and 284, the smallest pair of amicable numbers.

4. The are_amicable function is used to determine if num_a and num_b form an amicable pair.

5. The result is printed, confirming that 220 and 284 are indeed amicable numbers because the sum of the proper divisors of each number equals the other number.