Two different positive integers a and b form an amicable pair when each is the sum of the other’s proper divisors.
Remember
a and b are amicable ↔ s(a) = b and s(b) = a and a ≠ b
s(n) = sum of proper divisors of n
= positive divisors of n except n itself
Classic pair: 220 and 284
s(220) = 284
s(284) = 220
a
b
s(a)
s(b)
Result
220
284
284
220
Amicable
1184
1210
1210
1184
Amicable
28
28
28
28
Perfect (not a pair)
220
221
284
1
Not amicable
Approach
How to Check Amicable Numbers in C++
Compute both proper-divisor sums, then verify the cross equalities.
Reject the case a == b (that would be perfect, not amicable).
Compute s(a) and s(b) (sum of divisors strictly less than the number).
Return true only when s(a) == b and s(b) == a.
Method
Idea
Time
Best for
Loop to n/2
Clear s(n), then check both ways
O(a + b)
Interviews, clarity
√n pairs
Faster s(n)
O(√a + √b)
Larger inputs
Find partner
b = s(a), verify s(b) == a
O(a) or O(√a)
One known value
Pseudocode
Pseudocode
function ProperDivisorSum(n):
if n <= 1:
return 0
sum = 0
for i from 1 to 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
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Rule
s(a)=b and s(b)=a, a≠b
s(n)
Sum of divisors strictly less than n
Classic pair
(220, 284)
Equals
Not amicable — check perfect instead
One direction
Not enough — verify both ways
Partner of a
b = s(a), then check s(b)=a
Try it
Live Preview
Enter two positive integers to compare each proper-divisor sum with the other value.
Positive whole numbers. Tap a chip or type a pair.
Result
a = 220, b = 284
s(220) = 284
s(284) = 220
Verdict: AMICABLE
Trace
Worked Walkthrough — 220 and 284
Compute each proper-divisor sum, then check both cross equalities.
Calculation
Proper divisors of 220:
1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
Sum = 284
Proper divisors of 284:
1, 2, 4, 71, 142
Sum = 220
220 ≠ 284, s(220)=284, s(284)=220
→ amicable pair
Code
C++ Programs
Start with the clear n/2 sum. Then speed it up with √n pairs, and finish by finding a partner from one value.
Example 1 — Basic Pair Check
Sum proper divisors with a loop to num / 2, then test both directions.
C++
#include <iostream>
int properDivisorSum(int num) {
if (num <= 1) {
return 0;
}
int total = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
total += i;
}
}
return total;
}
bool areAmicable(int a, int b) {
if (a == b) {
return false;
}
return properDivisorSum(a) == b && properDivisorSum(b) == a;
}
int main() {
int a = 220;
int b = 284;
if (areAmicable(a, b)) {
std::cout << a << " and " << b << " are amicable numbers.\n";
} else {
std::cout << a << " and " << b << " are not amicable numbers.\n";
}
return 0;
}
Output
220 and 284 are amicable numbers.
How It Works
properDivisorSum adds every divisor strictly less than num.
areAmicable returns false when both inputs are equal.
It then requires s(a) == b and s(b) == a.
For 220 and 284 both cross equalities hold.
This is the clearest interview version.
Example 2 — Optimized with Divisor Pairs
Walk i up to √n and add both factors (skipping n itself).
C++
#include <iostream>
int properDivisorSum(int num) {
if (num <= 1) {
return 0;
}
int total = 1;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
total += i;
int pair = num / i;
if (pair != i) {
total += pair;
}
}
}
return total;
}
bool areAmicable(int a, int b) {
if (a == b) {
return false;
}
return properDivisorSum(a) == b && properDivisorSum(b) == a;
}
int main() {
int a = 220;
int b = 284;
if (areAmicable(a, b)) {
std::cout << a << " and " << b << " are amicable numbers.\n";
} else {
std::cout << a << " and " << b << " are not amicable numbers.\n";
}
return 0;
}
Output
220 and 284 are amicable numbers.
How It Works
Seed the sum with 1 for every n > 1.
Loop while i * i <= num and add each factor pair.
When i == num / i, add the square root only once.
Partner num / i is never num itself because i starts at 2.
The amicable check is unchanged — only s(n) is faster.
Example 3 — Find Amicable Partner of a
Compute b = s(a), then confirm s(b) == a and a != b.
C++
#include <iostream>
int properDivisorSum(int num) {
if (num <= 1) {
return 0;
}
int total = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
total += i;
}
}
return total;
}
// Returns the partner, or -1 when none exists.
int amicablePartner(int a) {
int b = properDivisorSum(a);
if (a != b && properDivisorSum(b) == a) {
return b;
}
return -1;
}
int main() {
int a = 220;
int partner = amicablePartner(a);
if (partner != -1) {
std::cout << "Partner of " << a << " is " << partner << ".\n";
} else {
std::cout << a << " has no amicable partner.\n";
}
return 0;
}
Output
Partner of 220 is 284.
How It Works
The only candidate partner is b = s(a).
Reject the case a == b (perfect numbers).
Verify the reverse: s(b) == a.
Return b on success, or -1 when no partner exists.
For 220, the partner is 284.
Edge Cases & Pitfalls
Check these before calling the solution done.
Equals
Not amicable
If a == b, you are looking at a perfect-number test, not a pair.
One way
Check both directions
s(a) == b alone is not enough — you also need s(b) == a.
s(n)
Exclude n itself
Proper divisors never include the number. Including it breaks every check.
1
s(1) = 0
1 has no proper divisors under the usual convention. Guard n <= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Basic pair check
O(a + b)
O(1)
Sqrt pair check
O(√a + √b)
O(1)
Find partner
O(a) or O(√a)
O(1)
Explain the simple loop first; mention √n when asked about larger inputs.
Remember
Key Takeaways
Definition:s(a)=b, s(b)=a, and a≠b.
Classic: 220 ↔ 284.
Tool: reuse s(n) for perfect, abundant, and amicable checks.
Trap: one-way equality or a == b is not amicable.
One line: each number’s proper-divisor sum must equal the other — and they must differ.
Frequently Asked Questions
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, 284).
No. That would mean s(a) = a, which is a perfect number, not an amicable pair. The definition requires two distinct numbers.
s(n) is the sum of proper divisors of n — all positive divisors of n that are smaller than n.
All three use s(n). Perfect: s(n)=n. Abundant: s(n)>n. Amicable: s(a)=b and s(b)=a with a≠b.
A clear loop to n/2 per value is O(a+b). Using divisor pairs up to √n makes one pair check O(√a + √b).