Two different positive integers a and b form an amicable pair when each equals the other’s proper-divisor sum (aliquot sum), written s(n).
Remember
s(n) = sum of proper divisors of n
a ≠ b
s(a) = b and s(b) = a → amicable pair
Proper divisors = positive divisors of n, except n itself
Example: s(220) = 284, s(284) = 220 → amicable
Concept
Condition
Example
Amicable pair
s(a)=b, s(b)=a, a≠b
220 & 284
Perfect
s(n) = n
6, 28
Abundant
s(n) > n
12 (not a pair)
Well-known amicable pairs
Golden tests for interviews and unit checks — start with 220 and 284.
Four clear steps — share one proper-divisor helper for both numbers.
Reject if a < 2, b < 2, or a == b.
Compute s(a) and s(b) (sum proper divisors).
Require s(a) == bands(b) == a.
Optionally print both sums so the two-way link is visible.
Method
Idea
Time
Best for
Basic loop
Sum divisors from 1 to n / 2
O(a + b)
Interviews, small values
Divisor pairs
Loop to √n; add i and n / i (skip n)
O(√a + √b)
Larger inputs, optimizations
Pseudocode
Pseudocode
function SumProperDivisors(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 < 2 or b < 2 or a == b:
return false
return SumProperDivisors(a) == b and SumProperDivisors(b) == a
Cheat sheet
Goal
Pattern
Is divisor?
n % i == 0
Basic bound
for (i = 1; i <= n / 2; i++)
Amicable test
s(a) == b && s(b) == a && a != b
Not perfect
Reject a == b even if s(a) == a
Try it
Live Preview
Enter two positive integers to see s(a), s(b), and the amicable verdict.
Whole numbers ≥ 1 (capped at 999999). Order does not matter for a valid pair.
Result
a = 220, b = 284
s(220) = 284
s(284) = 220
a ≠ b: true
s(a) == b: true
s(b) == a: true
Verdict: AMICABLE PAIR
Trace
Worked Walkthrough — 220 & 284
Compute s(220) with a loop to 220 / 2 = 110, then check s(284).
Proper divisors of 220
Divisors added
Running sum
1, 2, 4, 5, 10, 11
33
+ 20, 22, 44, 55, 110
284
s(220) = 284 → matches partner b.
Proper divisors of 284
i
284 % i
Action
sum
1
0
Add 1
1
2
0
Add 2
3
4
0
Add 4
7
71
0
Add 71
78
142
0
Add 142
220
Final check: s(220) = 284, s(284) = 220, and 220 ≠ 284 → amicable pair (the smallest one).
Code
C# Programs
Three programs: check a fixed pair, user input with both sums printed, and an O(√n) helper. Use View Output for sample results.
Example 1 — Check One Pair
Shared SumProperDivisors helper, then the two-way amicable test.
C#
using System;
class Program
{
static int SumProperDivisors(int n)
{
if (n <= 1)
{
return 0;
}
int sum = 0;
for (int i = 1; i <= n / 2; i++)
{
if (n % i == 0)
{
sum += i;
}
}
return sum;
}
static bool AreAmicable(int a, int b)
{
if (a < 2 || b < 2 || a == b)
{
return false;
}
return SumProperDivisors(a) == b && SumProperDivisors(b) == a;
}
static void Main()
{
int a = 220;
int b = 284;
if (AreAmicable(a, b))
{
Console.WriteLine(a + " and " + b + " are amicable numbers.");
}
else
{
Console.WriteLine(a + " and " + b + " are not amicable numbers.");
}
}
}
Output
220 and 284 are amicable numbers.
How It Works
1. Proper-divisor sum. Loop i from 1 to n / 2 and add every divisor. Never add n itself.
2. Guard the pair. Reject values below 2 and reject a == b so perfect numbers are not treated as amicable.
3. Two-way check. Require s(a) == b and s(b) == a. For 220 and 284 both sides hold.
Example 2 — User Input + Both Sums
Interview follow-up: read two integers safely, print s(a) and s(b), then the verdict.
C#
using System;
class Program
{
static int SumProperDivisors(int n)
{
if (n <= 1)
{
return 0;
}
int sum = 0;
for (int i = 1; i <= n / 2; i++)
{
if (n % i == 0)
{
sum += i;
}
}
return sum;
}
static void Main()
{
Console.Write("Enter first number (a): ");
if (!int.TryParse(Console.ReadLine(), out int a) || a < 1)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
Console.Write("Enter second number (b): ");
if (!int.TryParse(Console.ReadLine(), out int b) || b < 1)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
int sa = SumProperDivisors(a);
int sb = SumProperDivisors(b);
Console.WriteLine("s(" + a + ") = " + sa);
Console.WriteLine("s(" + b + ") = " + sb);
if (a != b && sa == b && sb == a)
{
Console.WriteLine(a + " and " + b + " form an amicable pair.");
}
else
{
Console.WriteLine(a + " and " + b + " do not form an amicable pair.");
}
}
}
Output (when user enters 220 then 284)
Enter first number (a): 220
Enter second number (b): 284
s(220) = 284
s(284) = 220
220 and 284 form an amicable pair.
How It Works
1. Safe input.int.TryParse avoids FormatException on bad text.
2. Show the sums. Printing s(a) and s(b) makes the link obvious when debugging wrong answers.
3. Same rule. Still require a != b, sa == b, and sb == a. Try 6 and 6: sums match n, but equal inputs are not an amicable pair.
Example 3 — O(√n) Proper-Divisor Helper
Same amicable test; faster sum using divisor pairs up to √n.
C#
using System;
class Program
{
static int SumProperDivisorsFast(int n)
{
if (n <= 1)
{
return 0;
}
int sum = 1; // 1 is always a proper divisor for n > 1
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
sum += i;
int partner = n / i;
if (partner != i && partner != n)
{
sum += partner;
}
}
}
return sum;
}
static bool AreAmicableFast(int a, int b)
{
if (a < 2 || b < 2 || a == b)
{
return false;
}
return SumProperDivisorsFast(a) == b
&& SumProperDivisorsFast(b) == a;
}
static void Main()
{
Console.WriteLine(AreAmicableFast(220, 284)); // True
Console.WriteLine(AreAmicableFast(1184, 1210)); // True
Console.WriteLine(AreAmicableFast(6, 6)); // False (perfect)
}
}
Output
True
True
False
How It Works
1. Seed with 1. Start the sum at 1 and begin the loop at 2.
2. Walk to √n. While i * i <= n, each factor i has a partner n / i.
3. Add carefully. Always add i. Add partner only when it differs from i and is not n. Same amicable rule as Example 1 — faster sums. Explain the basic helper first in interviews.
Edge Cases & Pitfalls
Check these before calling the solution done.
a == b
Not a pair
Equal inputs are never amicable — that path is for perfect numbers.
One way
Check both
s(a) == b alone is not enough; also require s(b) == a.
Include n?
Never add n
Including n in the sum breaks every amicable and perfect check.
Order
220, 284 = 284, 220
A valid pair is commutative; your function should accept either order.
n < 2
Reject early
Return false when either value is below 2.
Squares
No double-count
In pair mode, add the square root only once when i * i == n.
Analysis
Time and Space Complexity
Program
Time
Extra space
Basic loops to n/2
O(a + b)
O(1)
Divisor pairs up to √n
O(√a + √b)
O(1)
Scan all pairs in 1…m
O(m² · √m) with fast sum
O(1)
Remember
Key Takeaways
Definition:s(a)=b, s(b)=a, and a≠b.
Helper first: one proper-divisor sum function powers the whole solution.
One line: compute both proper-divisor sums; if each equals the other number and they differ, the pair is amicable.
Frequently Asked Questions
Two different positive integers a and b are amicable when the sum of proper divisors of a equals b, and the sum of proper divisors of b equals a. Example: 220 and 284.
s(n) is the aliquot (proper-divisor) sum of n — all positive divisors of n except n itself.
No. Amicable numbers must be different. If s(n) equals n, the number is perfect, not amicable.
s(a) == b alone is not enough. You also need s(b) == a and a != b. One-way matches are not amicable pairs.
Abundant checks one number: s(n) > n. Amicable checks two numbers: s(a) = b and s(b) = a.
Perfect means s(n) = n (one number). Amicable means two different numbers each equal the other's proper-divisor sum.
No. s(1) is 0 in this convention, and amicable pairs start much larger (220, 284).
Write a clear SumOfProperDivisors helper with a loop to n/2, then show the O(√n) pair version if asked to optimize.
Basic loops to n/2 cost O(a + b). Divisor pairs up to √n cost O(√a + √b). Extra space is O(1).
Assert (220, 284) true, (6, 6) false (perfect, not a pair), (12, 18) false, and optionally (1184, 1210) true.
🤔
Did you know?
The smallest amicable pair is 220 and 284. Next well-known pairs include 1184 & 1210 and 2620 & 2924.