Two different positive integers a and b are an amicable pair when each equals the sum of the other’s proper divisors.
Remember
s(n) = sum of proper divisors of n
a and b are amicable ↔
a ≠ b and s(a) = b and s(b) = a
Classic pair: 220 and 284
s(220) = 284
s(284) = 220
a
b
Result
220
284
Amicable
1184
1210
Amicable
28
28
Not amicable (perfect)
220
221
Not amicable
Approach
How to Check Amicable Numbers in JavaScript
Four clear steps — both directions matter.
Reject the case a == b (that belongs to perfect numbers).
Compute s(a) = sum of proper divisors of a.
Compute s(b) the same way.
Accept only when s(a) == b and s(b) == a.
Method
Idea
Time
Best for
Loop to n/2
Clear proper-divisor sum
O(a+b)
Interviews, beginners
Pairs to √n
Faster s(n)
O(√a+√b)
Larger inputs
Find partner
b = s(a), verify reverse
O(√a)
Given one number
Pseudocode
Pseudocode
function SumProper(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 SumProper(a) == b and SumProper(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
Code
JavaScript Programs
Start with the clear n/2 sum. Then speed it up with √n pairs, and finish by finding a partner from one value. Each example has a Try it Yourself editor.
Example 1 — Basic Pair Check
Sum proper divisors with a loop to num / 2, then test both directions.
JavaScript
function sumOfDivisors(num) {
if (num <= 1) {
return 0;
}
let sum = 0;
for (let i = 1; i <= Math.floor(num / 2); i++) {
if (num % i === 0) {
sum += i;
}
}
return sum;
}
function areAmicable(num1, num2) {
if (num1 === num2) {
return false;
}
return sumOfDivisors(num1) === num2 && sumOfDivisors(num2) === num1;
}
const a = 220;
const b = 284;
if (areAmicable(a, b)) {
console.log(a + " and " + b + " are amicable numbers.");
} else {
console.log(a + " and " + b + " are not amicable numbers.");
}
sumOfDivisors 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).
JavaScript
function sumOfDivisors(num) {
if (num <= 1) {
return 0;
}
let sum = 1;
for (let i = 2; i * i <= num; i++) {
if (num % i === 0) {
sum += i;
if (i !== num / i) {
sum += num / i;
}
}
}
return sum;
}
function areAmicable(num1, num2) {
if (num1 === num2) {
return false;
}
return sumOfDivisors(num1) === num2 && sumOfDivisors(num2) === num1;
}
const a = 220;
const b = 284;
if (areAmicable(a, b)) {
console.log(a + " and " + b + " are amicable numbers.");
} else {
console.log(a + " and " + b + " are not amicable numbers.");
}
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.
JavaScript
function sumOfDivisors(num) {
if (num <= 1) {
return 0;
}
let sum = 0;
for (let i = 1; i <= Math.floor(num / 2); i++) {
if (num % i === 0) {
sum += i;
}
}
return sum;
}
function amicablePartner(a) {
const b = sumOfDivisors(a);
if (a !== b && sumOfDivisors(b) === a) {
return b;
}
return null;
}
const a = 220;
const partner = amicablePartner(a);
if (partner !== null) {
console.log("Partner of " + a + " is " + partner + ".");
} else {
console.log(a + " has no amicable partner.");
}
Return b on success, or null 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: proper-divisor sum — same building block as perfect/abundant.
Test set: (220,284) yes; (28,28) no; (220,221) no.
One line: each number is the proper-divisor sum of the other — and they must be different.
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).
Use the Try it Yourself links under each code sample — they open an in-browser editor with the same logic so you can edit the inputs and Run.