- s(220)
- 284
- s(284)
- 220
Check Amicable Number in PHP
What you’ll learn
- What an amicable pair is in very simple words.
- How to compute the sum of proper divisors of a number.
- Two PHP programs: one optimized and one beginner-friendly.
- Edge cases, complexity, and common interview follow-up points.
Overview
Two numbers a and b are amicable when the sum of proper divisors of a is b, and the sum of proper divisors of b is a. They must be different numbers.
Prerequisites
You can follow this page if you are comfortable writing small PHP scripts.
- Basic PHP: variables, functions,
if, andforloops. - Modulo operator
%to test divisibility:$n % $i === 0. - The idea of proper divisors: positive divisors strictly less than the number.
What is an amicable pair?
Let s(n) mean: add all positive divisors of n that are smaller than n. Then a and b are amicable if s(a)=b and s(b)=a, with a != b.
Example: 220 and 284 are amicable. That is the smallest known amicable pair.
Intuition and examples
- Reason
- They are equal numbers; amicable needs two different numbers.
- Reason
s(10)=8ands(9)=4, so conditions fail.
Live preview
Enter values of a and b to check whether they form an amicable pair.
Algorithm
Goal: check if two positive integers form an amicable pair.
Compute s(a)
Add proper divisors of a.
Compute s(b)
Add proper divisors of b.
Check all conditions
Return true when a != b, s(a)==b, and s(b)==a.
📜 Pseudocode
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) == aCheck one pair (optimized with divisor pairs)
<?php
function properDivisorSum(int $num): int
{
if ($num <= 1) {
return 0;
}
$sum = 1;
for ($i = 2; $i * $i <= $num; $i++) {
if ($num % $i === 0) {
$sum += $i;
$pair = intdiv($num, $i);
if ($pair !== $i) {
$sum += $pair;
}
}
}
return $sum;
}
function areAmicable(int $a, int $b): bool
{
if ($a === $b) {
return false;
}
return properDivisorSum($a) === $b && properDivisorSum($b) === $a;
}
$a = 220;
$b = 284;
echo areAmicable($a, $b)
? "$a and $b are amicable numbers."
: "$a and $b are not amicable numbers.";
?>Same pair check (basic method)
<?php
function properDivisorSumBasic(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 areAmicableBasic(int $a, int $b): bool
{
if ($a === $b) {
return false;
}
return properDivisorSumBasic($a) === $b && properDivisorSumBasic($b) === $a;
}
$a = 220;
$b = 284;
echo areAmicableBasic($a, $b)
? "$a and $b are amicable numbers."
: "$a and $b are not amicable numbers.";
?>❓ FAQ
Edge cases and pitfalls
Not amicable
Same numbers are excluded in the standard definition.
Need both directions
You must check both s(a)==b and s(b)==a.
Use 0 sum
Return 0 for divisor sum helper when number is 1 or less.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Basic divisor sum | O(a + b) | O(1) |
| Sqrt divisor pairing | O(√a + √b) | O(1) |
Summary
- Definition: Amicable needs two different numbers and two divisor-sum checks.
- Code: Write a proper-divisor-sum function, then compare both directions.
- Optimization: Divisor pairs up to sqrt(n) are faster for large numbers.
The smallest amicable pair is 220 and 284. The sum of proper divisors of 220 is 284, and the sum of proper divisors of 284 is 220.
9 people found this page helpful
