Check Amicable Number in Python

Beginner
⏱️ 13 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What an amicable pair is in very simple words.
  • How to compute the sum of proper divisors of a number.
  • Two Python programs: one optimized and one beginner-friendly.
  • Edge cases, complexity, and common interview follow-up points.

Overview

Two numbers a and b are amicable when the sum of proper divisors of a is b, and the sum of proper divisors of b is a. They must be different numbers.

What is an amicable pair?

Let s(n) mean: add all positive divisors of n that are smaller than n. Then a and b are amicable if s(a)=b and s(b)=a, with a != b.

Example: 220 and 284 are amicable. That is the smallest known amicable pair.

Intuition and examples

220 & 284Amicable
s(220)
284
s(284)
220
6 & 6Not amicable
Reason
They are equal numbers; amicable needs two different numbers.
10 & 9Not amicable
Reason
s(10)=8 and s(9)=4, so conditions fail.

Live preview

Enter values of a and b to check whether they form an amicable pair.

Use whole numbers a, b ≥ 1.

Live result
Press "Run check" to see s(a), s(b), and the verdict.

Algorithm

Goal: check if two positive integers form an amicable pair.

Compute s(a)

Add proper divisors of a.

Compute s(b)

Add proper divisors of b.

Check all conditions

Return true when a != b, s(a)==b, and s(b)==a.

📜 Pseudocode

Pseudocode
function properDivisorSum(n):
    if n <= 1:
        return 0
    sum = 0
    for i from 1 to floor(n/2):
        if n mod i == 0:
            sum = sum + i
    return sum

function areAmicable(a, b):
    if a == b:
        return false
    return properDivisorSum(a) == b and properDivisorSum(b) == a
1

Check one pair (optimized with divisor pairs)

python
def proper_divisor_sum(num: int) -> int:
    if num <= 1:
        return 0
    total = 1
    i = 2
    while i * i <= num:
        if num % i == 0:
            total += i
            pair = num // i
            if pair != i:
                total += pair
        i += 1
    return total


def are_amicable(a: int, b: int) -> bool:
    if a == b:
        return False
    return proper_divisor_sum(a) == b and proper_divisor_sum(b) == a


a, b = 220, 284
if are_amicable(a, b):
    print(f"{a} and {b} are amicable numbers.")
else:
    print(f"{a} and {b} are not amicable numbers.")
2

Same pair check (basic method)

python
def proper_divisor_sum_basic(num: int) -> int:
    if num <= 1:
        return 0
    total = 0
    for i in range(1, num // 2 + 1):
        if num % i == 0:
            total += i
    return total


def are_amicable_basic(a: int, b: int) -> bool:
    if a == b:
        return False
    return proper_divisor_sum_basic(a) == b and proper_divisor_sum_basic(b) == a


a, b = 220, 284
if are_amicable_basic(a, b):
    print(f"{a} and {b} are amicable numbers.")
else:
    print(f"{a} and {b} are not amicable numbers.")

❓ FAQ

Two different positive integers a and b are amicable if the sum of proper divisors of a equals b, and the sum of proper divisors of b equals a. The smallest pair is 220 and 284.
No. Amicable numbers must be different. If a number equals the sum of its own proper divisors, it is called a perfect number.
Because 1 has no proper divisors in this convention, and values below 1 are not used in this number theory definition.
Abundant uses one number and checks if s(n) > n. Amicable uses two numbers and checks s(a)=b and s(b)=a.
With sqrt divisor pairing it is O(sqrt(a) + sqrt(b)). With the basic loop to n//2 it is O(a + b).
Start with the simple loop method for clarity, then mention the sqrt optimization for large inputs.

Edge cases and pitfalls

a == b

Not amicable

Same numbers are excluded in the standard definition.

One-way check

Need both directions

You must check both s(a)==b and s(b)==a.

n <= 1

Use 0 sum

Return 0 for divisor sum helper when number is 1 or less.

⏱️ Time and space complexity

ApproachTimeExtra space
Basic divisor sumO(a + b)O(1)
Sqrt divisor pairingO(√a + √b)O(1)

Summary

  • Definition: Amicable needs two different numbers and two divisor-sum checks.
  • Code: Write a proper-divisor-sum function, then compare both directions.
  • Optimization: Divisor pairs up to sqrt(n) are faster for large numbers.
Did you know?

The smallest amicable pair is 220 and 284. The sum of proper divisors of 220 is 284, and the sum of proper divisors of 284 is 220.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful