Check Perfect Number in PHP

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

What you’ll learn

  • What a perfect number is, and how it sits beside deficient and abundant numbers.
  • How to sum proper divisors in PHP with a simple loop and the % operator.
  • A live preview that shows divisors and the verdict before you compile.
  • Two complete programs: test one value (for example 28) and list perfect numbers in a range.

Overview

Picture a positive whole number n. List every proper divisor—positive pieces that divide n evenly and are smaller than n. Add those pieces. If the total equals n exactly, the number is perfect. The smallest example is 6, because 1 + 2 + 3 = 6.

Two programs

One answers “Is this number perfect?” The other scans a small interval (such as 1 to 50) and prints every hit.

Live preview

Type any modest n and see the divisor list, the sum, and whether the outcome is perfect, abundant, or deficient.

Interview-ready

Clean loop structure, clear edge case for n = 1, and optional speed-ups if the examiner asks about larger inputs.

Prerequisites

You should be comfortable with tiny PHP programs and what “divides evenly” means.

  • Basic PHP syntax, echo, for loops, and small helper functions.
  • The remainder operator %: n % i == 0 means i is a divisor of n.
  • Idea of a divisor: a whole number that splits another whole number without leaving a remainder.

What is a perfect number?

A perfect number is a positive integer n whose proper divisors add up to n itself. “Proper” means we skip n when we list divisors—we only use smaller ones.

Write s(n) for that sum. Greek mathematicians sorted numbers into three familiar buckets:

Deficient s(n) < n
Perfect s(n) = n
Abundant s(n) > n

Same idea, two formulas

Books often write σ(n) for the sum of every positive divisor of n, including n. The sum of proper divisors is s(n) = σ(n) − n. Saying “n is perfect” is the same as either s(n) = n or σ(n) = 2n—multiply the second equation out and you will see why.

Tiny checklist

Add proper divisors. If the total equals n, print “perfect.” If the total is smaller, call it deficient; if larger, abundant. One loop can classify all three.

Numbers to try in your head

Each card shows proper divisors and the sum s(n), so you can see how the definition feels on real examples.

6 Perfect
Proper divisors
1, 2, 3
Sum s(n)
1 + 2 + 3 = 6
Verdict
s(6) = 6 — the smallest perfect number.
28 Perfect
Proper divisors
1, 2, 4, 7, 14
Sum s(n)
1 + 2 + 4 + 7 + 14 = 28
Verdict
The next perfect number after 6; our sample programs use it often.
10 Deficient
Proper divisors
1, 2, 5
Sum s(n)
1 + 2 + 5 = 8
Verdict
8 < 10 — not perfect.
12 Abundant
Proper divisors
1, 2, 3, 4, 6
Sum s(n)
1 + 2 + 3 + 4 + 6 = 16
Verdict
16 > 12 — abundant (see the abundant number tutorial).

Live preview

This mirrors the PHP logic: walk from 1 to n/2, collect every divisor, add them, compare to n. Try 6, 28, 10, or 12 to see perfect versus deficient versus abundant.

Use whole numbers n ≥ 1. Values above 999 999 are blocked so the tab stays responsive.

Live result
Press “Run check” to see proper divisors, s(n), and whether n is perfect.

Algorithm

Goal: decide whether a positive integer n is perfect by computing the sum of its proper divisors.

Start with sum = 0

You will add each proper divisor into this running total.

Try every candidate from 1 to n/2

If n % i == 0, then i is a proper divisor (the loop range keeps i < n automatically).

Compare sum to n

If sum == n, the number is perfect. Otherwise it is not (and you can also say whether it is deficient or abundant).

📜 Pseudocode

Pseudocode
function properDivisorSum(n):
    sum ← 0
    for i from 1 to floor(n / 2):
        if n mod i = 0:
            sum ← sum + i
    return sum

function isPerfect(n):
    if n < 2:
        return false   // 1 is not perfect by convention here
    return properDivisorSum(n) = n
1

Check one number

Classic interview layout: a helper returns true when the divisor sum hits n, and main prints a friendly sentence. The sample uses 28.

php
<?php
function isPerfectNumber(int $number): bool
{
    $sum = 0;
    for ($i = 1; $i <= (int)($number / 2); $i++) {
        if ($number % $i === 0) {
            $sum += $i;
        }
    }
    return $sum === $number;
}

$number = 28;

if (isPerfectNumber($number)) {
    echo $number . " is a perfect number.\n";
} else {
    echo $number . " is not a perfect number.\n";
}
?>

Explanation

The loop visits every integer that could possibly be a proper divisor. When number % i == 0, we add i. After the loop, sum equals s(number); equality with number means perfect.

2

Perfect numbers in a range

Reuse the same helper: loop candidate values from 1 to 50 and print each perfect number you meet. Only 6 and 28 appear in that window.

php
<?php
function isPerfectNumber(int $num): bool
{
    $sum = 0;
    for ($i = 1; $i <= (int)($num / 2); $i++) {
        if ($num % $i === 0) {
            $sum += $i;
        }
    }
    return $sum === $num;
}

echo "Perfect Numbers in the range 1 to 50:\n";
for ($i = 1; $i <= 50; $i++) {
    if (isPerfectNumber($i)) {
        echo $i . " ";
    }
}
echo "\n";
?>

Explanation

for (int i = 1; i <= num / 2; ++i) { if (num % i == 0) sum += i; }

Inner logic. Same divisor walk as Example 1; return sum == num is the perfect test.

for ($i = 1; $i <= 50; $i++) { if (isPerfectNumber($i)) echo $i . " "; }

Outer loop. Try each integer in the interval. Swap 50 for any upper bound you need for homework or extensions.

Notes

Faster divisor scan. You can stop at √n and add divisor pairs instead of walking all the way to n/2. Handle square roots carefully so you do not double-count.

Overflow. For large n, the divisor sum can exceed a 32-bit int. For very large values, consider big-integer/string approaches depending on your runtime.

Rarity. Perfect numbers are sparse; brute-forcing huge ranges is slow and usually unnecessary in classroom tasks.

❓ FAQ

A counting number is perfect when you add up all of its smaller divisors (not counting the number itself) and that total exactly equals the original number. Example: 6 = 1 + 2 + 3.
No. It has no proper divisors in the usual sense, so the sum is treated as 0, which does not equal 1.
Same divisor sum s(n): if s(n) > n the number is abundant; if s(n) < n it is deficient; if s(n) = n it is perfect.
Every proper divisor of n is at most n/2 (the largest possible proper divisor of an even n is n/2). So checking 1 through n/2 finds every proper divisor once.
Yes. σ(n) sums all divisors including n, while s(n) sums only proper divisors. Since σ(n) = s(n) + n, the condition s(n) = n is the same as σ(n) = 2n.
No. They are very rare. That is why interview programs usually hard-code a test value like 28 or scan only a small range.

🔄 Input / output examples

Example 1 uses a fixed number in source. Swap it or read user input with trim(fgets(STDIN)) in PHP CLI.

Value testedTypical line printed (Example 1)
2828 is a perfect number.
1010 is not a perfect number.
66 is a perfect number.
1212 is not a perfect number.

Edge cases

These details keep your answer precise in labs and interviews.

n = 1

No proper divisors

The sum stays 0, which does not equal 1, so 1 is not perfect. If your assignment requires special wording, mention this explicitly.

Primes

Always deficient

A prime p > 1 has only 1 as a proper divisor, so s(p) = 1 < p. No prime is perfect.

Do not add n

Proper divisors only

If you accidentally include n in the sum, almost every number would look “too heavy” and the test breaks. The loop to n/2 avoids that mistake.

⏱️ Time and space complexity

TaskTimeExtra space
Test one n (loop to n/2)O(n)O(1)
Test one n (√n pairing)O(√n)O(1)
Scan 1 .. U with naive test each iO(U2)O(1)

Summary

  • Definition: n is perfect when the sum of its proper divisors equals n (same as σ(n) = 2n).
  • Code pattern: sum everything with n % i == 0 for i from 1 to n/2, then compare sum to n.
  • Remember: treat n = 1 separately, never add n into the divisor sum, and upgrade to wider integers if sums might overflow.
Did you know?

The first four perfect numbers are 6, 28, 496, and 8128. Mathematicians have known for centuries that every even perfect number fits a pattern tied to special primes; whether any odd perfect number exists is still an open question.

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