Check Amicable Number in PHP

Beginner
⏱️ 13 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

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, and for loops.
  • 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

220 & 284Amicable
s(220)
284
s(284)
220
6 & 6Not amicable
Reason
They are equal numbers; amicable needs two different numbers.
10 & 9Not amicable
Reason
s(10)=8 and s(9)=4, so conditions fail.

Live preview

Enter values of a and b to check whether they form an amicable pair.

Use whole numbers a, b ≥ 1.

Live result
Press "Run check" to see s(a), s(b), and the verdict.

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

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) == a
1

Check one pair (optimized with divisor pairs)

php
<?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.";
?>
2

Same pair check (basic method)

php
<?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

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 and 284.
No. Amicable numbers must be different. If a number equals the sum of its own proper divisors, it is called a perfect number.
Because 1 has no proper divisors in this convention, and values below 1 are not used in this number theory definition.
Abundant uses one number and checks if s(n) > n. Amicable uses two numbers and checks s(a)=b and s(b)=a.
With sqrt divisor pairing it is O(sqrt(a) + sqrt(b)). With the basic loop to n/2 it is O(a + b).
Start with the simple loop method for clarity, then mention the sqrt optimization for large inputs.

Edge cases and pitfalls

a == b

Not amicable

Same numbers are excluded in the standard definition.

One-way check

Need both directions

You must check both s(a)==b and s(b)==a.

n <= 1

Use 0 sum

Return 0 for divisor sum helper when number is 1 or less.

⏱️ Time and space complexity

ApproachTimeExtra space
Basic divisor sumO(a + b)O(1)
Sqrt divisor pairingO(√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.
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful