Pair Rule
s(a)=b, s(b)=a
Two different positives form an amicable pair when each equals the other’s proper-divisor sum.
Amicable numbers come in pairs: each is the proper-divisor sum of the other. This tutorial covers the definition, a live two-input preview, algorithm steps, worked C examples, edge cases, and complexity.
s(a)=b, s(b)=a
Two different positives form an amicable pair when each equals the other’s proper-divisor sum.
Exclude n
Add every positive divisor of n that is smaller than n — the shared building block.
220 & 284
The smallest amicable pair — your golden test for any implementation.
Two methods
Sum with a loop to n/2 for clarity, or divisor pairs to √n for speed.
Try a & b
Enter two numbers and see s(a), s(b), and the amicable verdict instantly.
Complexity
Both approaches use O(1) extra space; state both when interviewers ask.
An amicable pair is two different positive integers a and b such that the sum of proper divisors of a equals b, and the sum of proper divisors of b equals a.
Write s(n) for that proper-divisor sum. Then the conditions are simply a != b, s(a) == b, and s(b) == a. The famous first example is 220 and 284.
It reuses the same divisor-sum skill as perfect and abundant numbers, then adds a two-way relationship check that interviewers love to probe.
If a == b, you are looking at a perfect number — not amicable.
Need s(a)==b and s(b)==a — one way is not enough.
One sumOfDivisors powers the whole check.
Always verify your code against the smallest known pair.
In short: compute s(a) and s(b); if a ≠ b, s(a)=b, and s(b)=a, the numbers are amicable.
Given two positive integers a and b, decide whether they form an amicable pair.
/* Example: a = 220, b = 284
s(220) = 284
s(284) = 220
a != b → amicable pair */ | Item | Type | Description |
|---|---|---|
a, b | int | Two positive integers to test as a candidate pair. |
| Return / print | int (0/1) / text | 1 / message when they satisfy the amicable conditions. |
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 | Method | Idea | Time |
|---|---|---|
| Basic sum | Loop each number to n / 2 | O(a + b) |
| Sqrt pairs | Divisor pairs up to √n for each input | O(√a + √b) |
| Goal | Pattern |
|---|---|
| Proper-divisor sum | s(n) = sum of divisors of n that are < n |
| Amicable test | a != b and s(a) == b and s(b) == a |
| Basic upper bound | for (i = 1; i <= n / 2; ++i) |
| Reject equals | if (a == b) return 0; |
| Tiny n | s(n) = 0 when n <= 1 |
| Golden pair | 220 with 284 |
Same divisor-sum tool — different relationships.
s(a)=b, s(b)=aTwo different numbers linked by each other’s sums
s(n) = nOne number equals its own proper-divisor sum
s(n) > nOne number whose proper divisors overshoot it
reuse s(n)One helper covers all three problem families
Reach for amicable-pair drills when two-way divisor relationships matter.
Tests helper design, boolean conditions, and edge cases together.
Natural next step once students already know s(n).
“Find all amicable pairs below N” builds on the same check.
Shows why one-directional checks fail and both sides matter.
Brute force over large N is slow — discuss sieves or caching s(n) separately.
Key benefit: one clear pair problem that ties helper functions, two-way logic, and optional O(√n) speedups.
Enter a and b to see s(a), s(b), and whether they form an amicable pair.
Three complete C programs — basic check, sqrt-optimized check, and find a partner for one number. Click View Output to reveal sample console results.
Clearest version for whiteboards and beginners.
Sum proper divisors with a loop to num / 2, then test both directions.
#include <stdio.h>
int sumOfDivisors(int num) {
if (num <= 1) {
return 0;
}
int sum = 0;
for (int i = 1; i <= num / 2; ++i) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
int areAmicable(int num1, int num2) {
if (num1 == num2) {
return 0;
}
return sumOfDivisors(num1) == num2 && sumOfDivisors(num2) == num1;
}
int main(void) {
int a = 220;
int b = 284;
if (areAmicable(a, b)) {
printf("%d and %d are amicable numbers.\n", a, b);
} else {
printf("%d and %d are not amicable numbers.\n", a, b);
}
return 0;
} sumOfDivisors never includes the number itself. areAmicable rejects equal inputs (returns 0), then requires both cross equalities.
Same verdict with O(√n) divisor pairing.
Walk i up to √n and add both factors (skipping n itself).
#include <stdio.h>
/* Sum of proper divisors s(n); s(1) = 0 */
int sumOfDivisors(int num) {
if (num <= 1) {
return 0;
}
int sum = 1;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
return sum;
}
int areAmicable(int num1, int num2) {
if (num1 == num2) {
return 0;
}
return sumOfDivisors(num1) == num2 && sumOfDivisors(num2) == num1;
}
int main(void) {
int a = 220;
int b = 284;
if (areAmicable(a, b)) {
printf("%d and %d are amicable numbers.\n", a, b);
} else {
printf("%d and %d are not amicable numbers.\n", a, b);
}
return 0;
} Seed the sum with 1, then add each factor pair found below √n. When i * i == num, add the square root only once. The amicable check itself is unchanged.
Given one number, compute its candidate partner and verify.
Compute b = s(a), then confirm s(b) == a and a != b.
#include <stdio.h>
int sumOfDivisors(int num) {
if (num <= 1) {
return 0;
}
int sum = 0;
for (int i = 1; i <= num / 2; ++i) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
/* Returns partner if amicable, otherwise 0 */
int amicablePartner(int a) {
int b = sumOfDivisors(a);
if (a != b && sumOfDivisors(b) == a) {
return b;
}
return 0;
}
int main(void) {
int a = 220;
int partner = amicablePartner(a);
if (partner != 0) {
printf("Partner of %d is %d.\n", a, partner);
} else {
printf("%d has no amicable partner.\n", a);
}
return 0;
} The partner candidate is always s(a). You still must verify the reverse sum and that a is not perfect (where s(a) == a). This C version returns 0 when no partner exists.
If a == b, return 0 — that case belongs to perfect numbers.
Sum proper divisors of a with the basic or sqrt helper.
Do the same for b, then compare both cross links.
Return true only when s(a)=b and s(b)=a with a ≠ b.
Trace proper-divisor sums for the classic pair. (Full divisor lists are summarized; focus on the totals.)
| Number | Proper divisors (summary) | s(n) | Needed partner |
|---|---|---|---|
220 | 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 | 284 | 284 ✓ |
284 | 1, 2, 4, 71, 142 | 220 | 220 ✓ |
Also 220 != 284, so all three amicable conditions hold.
Where amicable-pair checks show up beyond the interview prompt.
Practice proper-divisor sums with a memorable story.
Example: introduce 220/284 in class.
Shows helper functions plus multi-condition returns.
Example: areAmicable(a, b) prompts.
Scan a range and collect unordered pairs once.
Example: all pairs with max < 10000.
Clarify why a == b is excluded from amicable.
Example: 6 is perfect, not amicable with itself.
Several classic problems ask for sums over amicable numbers.
Example: sum of all amicables under a limit.
Compare basic vs sqrt helpers on larger inputs.
Example: time both on five-digit pairs.
Pro Tip: keep sumOfDivisors pure and unit-test it with 220 → 284 and 284 → 220 before wiring the pair check.
Why this pattern works well in interviews and classwork.
s(a)=b and s(b)=a maps almost word-for-word into code.
The same sum function also solves perfect and abundant prompts.
Upgrade only the sum helper to O(√n) without touching the pair logic.
Pair checks need only a few integers — O(1) extra space.
Pro Tip: say the three conditions out loud (unequal, forward, reverse) before typing — it prevents one-way bugs.
Small habits that keep amicable code interview-ready.
Reject a == b immediately so perfect numbers never slip through.
s(a) == b alone is incomplete — include s(b) == a.
No printing inside sumOfDivisors — easier to reuse and test.
If that pair fails, fix the sum function before anything else.
When listing pairs, store unordered (min, max) so 220/284 appears once.
Pro Tip: for range searches, compute b = s(a) and only continue when b > a to avoid reporting each pair twice.
Mistakes that commonly break amicable-pair solutions.
Perfect numbers satisfy s(a)=a, which looks like a one-number “pair.”
→ Always require a != b.
s(a) == b without s(b) == a accepts many false positives.
→ Enforce both equalities.
Adding the number itself breaks every classic pair.
→ Loop to n / 2, or skip the partner when it equals n.
In pair mode, counting the root twice corrupts s(n).
→ Add the partner only when pair != i.
Range scanners often print both (220, 284) and (284, 220).
→ Keep only pairs with a < b.
Check these inputs before calling the solution done.
Same numbers are excluded; may be perfect instead.
Require s(a)==b and s(b)==a.
Match the convention used in this tutorial.
Golden test for any correct implementation.
s(6)=6, but a equals b.
Use divisor pairs when a or b gets large.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
0a < bareAmicable unchangedQuick Takeaway: a and b are amicable when they are different and each is the proper-divisor sum of the other.
| Program | Time | Extra space |
|---|---|---|
| Basic divisor sums | O(a + b) | O(1) |
| Sqrt divisor pairing | O(√a + √b) | O(1) |
| Find partner of one n | Same as one sum + one reverse sum | O(1) |
Amicable pairs are a clean two-way divisor-sum problem: compute s(a) and s(b), require a ≠ b, and match both directions. Master the basic helper first, then upgrade it to O(√n) when performance matters.
Practice the three examples above, then continue to Armstrong numbers for a different classic digit-power check.
Never skip the reverse check, never treat perfect numbers as amicable, and always verify 220 with 284.
a == b earlysumOfDivisors helperLink two integers the interview-friendly way.
s(a)=b and s(b)=a
Definitiona must differ from b
GuardsumOfDivisors
CodeDivisor pairs to √n
CodeO(a+b) or O(√)
AnalysisThe pair 220 and 284 is the smallest amicable pair: each number equals the sum of the proper divisors of the other. Pythagoras is said to have known of them; they appear in early Greek and Arab manuscripts as symbols of friendship.
Learn how to check whether a number equals the sum of its digits raised to a power.
9 people found this page helpful