- s(220)
- 284
- s(284)
- 220
Check Amicable Number in Python
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
- Reason
- They are equal numbers; amicable needs two different numbers.
- Reason
s(10)=8ands(9)=4, so conditions fail.
Live preview
Enter values of a and b to check whether they form an amicable pair.
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
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 Check one pair (optimized with divisor pairs)
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.") Same pair check (basic method)
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
Edge cases and pitfalls
Not amicable
Same numbers are excluded in the standard definition.
Need both directions
You must check both s(a)==b and s(b)==a.
Use 0 sum
Return 0 for divisor sum helper when number is 1 or less.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Basic divisor sum | O(a + b) | O(1) |
| Sqrt divisor pairing | O(√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.
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.
9 people found this page helpful
