- Proper divisors
- 1, 2, 3, 4, 6
- Sum
- 16 > 12
Check Abundant Number in PHP
What you’ll learn
- What an abundant number means in simple words.
- How to find proper divisors and their sum.
- One PHP program to check a single number and one to print a range.
- Edge cases, complexity, and interview-ready explanation points.
Overview
A number is abundant if its proper divisors add up to more than the number itself. This page gives clean, readable PHP code with clear logic so even beginners can explain it confidently.
Prerequisites
You only need basic PHP syntax to follow this page.
forloop,ifcondition, and modulo operator%.- Basic functions and
echooutput. - Understanding of divisors (numbers that divide exactly).
Quick examples
- Proper divisors
- 1, 2, 3
- Sum
- 6 = 6
- Proper divisors
- 1
- Sum
- 1 < 7
Live preview
Type a value to see its proper divisors, their sum, and final verdict.
Algorithm
Goal: find the sum of proper divisors and check if that sum is greater than n.
Handle tiny values
If n <= 1, return false.
Start sum at zero
Create $divSum = 0.
Scan divisors
Loop from 1 to intdiv($n, 2). If $n % $i === 0, add $i to $divSum.
Compare
If $divSum > $n, it is abundant; otherwise not abundant.
📜 Pseudocode
function isAbundant(n):
if n <= 1:
return false
divSum = 0
for i from 1 to floor(n / 2):
if n mod i == 0:
divSum = divSum + i
return divSum > nCheck one number
<?php
function isAbundant(int $num): bool
{
if ($num <= 1) {
return false;
}
$divSum = 0;
for ($i = 1; $i <= intdiv($num, 2); $i++) {
if ($num % $i === 0) {
$divSum += $i;
}
}
return $divSum > $num;
}
$number = 12;
if (isAbundant($number)) {
echo $number . " is an abundant number.";
} else {
echo $number . " is not an abundant number.";
}
?>Print abundant numbers from 1 to 50
<?php
function isAbundant(int $num): bool
{
if ($num <= 1) {
return false;
}
$divSum = 0;
for ($i = 1; $i <= intdiv($num, 2); $i++) {
if ($num % $i === 0) {
$divSum += $i;
}
}
return $divSum > $num;
}
echo "Abundant numbers between 1 and 50 are:\n";
for ($value = 1; $value <= 50; $value++) {
if (isAbundant($value)) {
echo $value . " ";
}
}
?>🔄 Input / output examples
Input n | Typical result |
|---|---|
| 12 | 12 is an abundant number. |
| 18 | 18 is an abundant number. |
| 7 | 7 is not an abundant number. |
| 1 | 1 is not an abundant number. |
❓ FAQ
Edge cases
Return false
These are not abundant in this tutorial definition.
Always not abundant
Prime numbers only have one proper divisor (1).
Use sqrt optimization
For very large values, divisor-pair logic is faster than scanning to n/2.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| Basic loop to n/2 | O(n) | O(1) |
| Divisor pairs up to sqrt(n) | O(√n) | O(1) |
Summary
- Definition: abundant means sum of proper divisors is greater than the number.
- Code: loop through divisors, add them, compare with
n. - Interview tip: explain basic and optimized methods.
The smallest abundant number is 12 because its proper divisors are 1, 2, 3, 4, 6 and their sum is 16, which is greater than 12.
9 people found this page helpful
