Python Program to Check If Two Numbers are Amicable Numbers

Amicable numbers are two different numbers where the sum of proper divisors of each number equals the other number. For example, 220 and 284 are amicable because the proper divisors of 220 sum to 284, and the proper divisors of 284 sum to 220.

What are Proper Divisors?

Proper divisors are all positive divisors of a number except the number itself. For example, proper divisors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110.

Method 1: Using Mathematical Approach

This method finds divisors efficiently by checking only up to the square root of the number ?

import math

def sum_of_divisors(num):
    divisor_sum = 1  # 1 is always a proper divisor
    
    for i in range(2, int(math.sqrt(num)) + 1):
        if num % i == 0:
            if i == num // i:  # Perfect square case
                divisor_sum += i
            else:
                divisor_sum += i + num // i
    
    return divisor_sum

def are_amicable(x, y):
    return sum_of_divisors(x) == y and sum_of_divisors(y) == x

# Test with known amicable pair
num1 = 220
num2 = 284

print(f"Testing numbers: {num1} and {num2}")
print(f"Sum of proper divisors of {num1}: {sum_of_divisors(num1)}")
print(f"Sum of proper divisors of {num2}: {sum_of_divisors(num2)}")

if are_amicable(num1, num2):
    print(f"{num1} and {num2} are amicable numbers")
else:
    print(f"{num1} and {num2} are not amicable numbers")
Testing numbers: 220 and 284
Sum of proper divisors of 220: 284
Sum of proper divisors of 284: 220
220 and 284 are amicable numbers

Method 2: Simple Brute Force Approach

This method checks all numbers from 1 to n-1 to find proper divisors ?

def find_proper_divisors_sum(n):
    divisor_sum = 0
    for i in range(1, n):
        if n % i == 0:
            divisor_sum += i
    return divisor_sum

def check_amicable_simple(a, b):
    sum_a = find_proper_divisors_sum(a)
    sum_b = find_proper_divisors_sum(b)
    
    return sum_a == b and sum_b == a

# Test with different pairs
pairs = [(220, 284), (1184, 1210), (100, 200)]

for x, y in pairs:
    result = check_amicable_simple(x, y)
    print(f"{x} and {y}: {'Amicable' if result else 'Not Amicable'}")
220 and 284: Amicable
1184 and 1210: Amicable
100 and 200: Not Amicable

How It Works

The algorithm works by:

  • Finding all proper divisors of the first number
  • Calculating their sum
  • Checking if this sum equals the second number
  • Repeating the process for the second number
  • Both conditions must be true for numbers to be amicable

Comparison

Method Time Complexity Best For
Mathematical (Method 1) O(?n) Large numbers
Brute Force (Method 2) O(n) Small numbers, learning

Finding Multiple Amicable Pairs

Here's how to find amicable pairs within a range ?

def find_amicable_pairs(limit):
    pairs = []
    
    for i in range(1, limit):
        sum_i = sum_of_divisors(i)
        
        if sum_i > i and sum_i < limit:  # Avoid duplicates and check range
            sum_j = sum_of_divisors(sum_i)
            if sum_j == i:
                pairs.append((i, sum_i))
    
    return pairs

# Find amicable pairs up to 3000
amicable_pairs = find_amicable_pairs(3000)
print("Amicable pairs found:")
for pair in amicable_pairs:
    print(f"{pair[0]} and {pair[1]}")
Amicable pairs found:
220 and 284
1184 and 1210

Conclusion

Amicable numbers are fascinating mathematical objects where two numbers are "friends" through their divisors. The mathematical approach using square root optimization is more efficient for larger numbers, while the brute force method is simpler to understand and implement.

Updated on: 2026-03-25T19:23:12+05:30

934 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements