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 PHP
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
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
PHP 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.
PHP
<?php
function sumOfDivisors(int $num): int {
if ($num <= 1) {
return 0;
}
$sum = 0;
for ($i = 1; $i <= intdiv($num, 2); $i++) {
if ($num % $i === 0) {
$sum += $i;
}
}
return $sum;
}
function areAmicable(int $num1, int $num2): bool {
if ($num1 === $num2) {
return false;
}
return sumOfDivisors($num1) === $num2
&& sumOfDivisors($num2) === $num1;
}
$a = 220;
$b = 284;
if (areAmicable($a, $b)) {
echo $a . " and " . $b . " are amicable numbers.\n";
} else {
echo $a . " and " . $b . " are not amicable numbers.\n";
}
?>
Output
220 and 284 are amicable numbers.
How It Works
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 $num itself).
PHP
<?php
function sumOfDivisors(int $num): int {
if ($num <= 1) {
return 0;
}
$sum = 1;
for ($i = 2; $i * $i <= $num; $i++) {
if ($num % $i === 0) {
$sum += $i;
if ($i !== intdiv($num, $i)) {
$sum += intdiv($num, $i);
}
}
}
return $sum;
}
function areAmicable(int $num1, int $num2): bool {
if ($num1 === $num2) {
return false;
}
return sumOfDivisors($num1) === $num2
&& sumOfDivisors($num2) === $num1;
}
$a = 220;
$b = 284;
if (areAmicable($a, $b)) {
echo $a . " and " . $b . " are amicable numbers.\n";
} else {
echo $a . " and " . $b . " are not amicable numbers.\n";
}
?>
Output
220 and 284 are amicable numbers.
How It Works
Seed the sum with 1 for every $num > 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.
PHP
<?php
function sumOfDivisors(int $num): int {
if ($num <= 1) {
return 0;
}
$sum = 0;
for ($i = 1; $i <= intdiv($num, 2); $i++) {
if ($num % $i === 0) {
$sum += $i;
}
}
return $sum;
}
/** Returns partner if amicable, otherwise 0 */
function amicablePartner(int $a): int {
$b = sumOfDivisors($a);
if ($a !== $b && sumOfDivisors($b) === $a) {
return $b;
}
return 0;
}
$a = 220;
$partner = amicablePartner($a);
if ($partner !== 0) {
echo "Partner of " . $a . " is " . $partner . ".\n";
} else {
echo $a . " has no amicable partner.\n";
}
?>
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 0 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 $num <= 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).