- Proper divisors
- 1, 2, 3
- Sum
- 6
Check Perfect Number in JavaScript
What you’ll learn
- What a perfect number is, and how it sits beside deficient and abundant numbers.
- How to sum proper divisors in JavaScript with a simple loop and the
%operator. - A live preview that shows divisors and the verdict before you run code snippets.
- Two complete programs: test one value and list perfect numbers in a range.
Overview
List every proper divisor of n (all positive divisors less than n) and add them. If the total equals n, the number is perfect. The smallest example is 6, because 1 + 2 + 3 = 6.
Two programs
One checks a single value; another scans a small range and prints all perfect hits.
Live preview
Type a number and instantly see divisor list, sum, and perfect/abundant/deficient classification.
Interview-ready
Clear loop logic and edge-case handling for n = 1.
Prerequisites
Comfort with loops, modulo arithmetic, and simple helper functions.
- Basic JavaScript syntax, functions, and
console.log. - Understanding that
n % i === 0meansidividesnevenly.
What is a perfect number?
A perfect number is a positive integer n whose proper divisors add up exactly to n.
With s(n) as proper-divisor sum:
Same idea, two formulas
If σ(n) is sum of all divisors (including n), then s(n) = σ(n) - n. So saying s(n) = n is the same as saying σ(n) = 2n.
Numbers to try in your head
- Proper divisors
- 1, 2, 4, 7, 14
- Sum
- 28
- Proper divisors
- 1, 2, 5
- Sum
- 8
- Proper divisors
- 1, 2, 3, 4, 6
- Sum
- 16
Live preview
Walks from 1 to n/2, sums proper divisors, and classifies the result.
Algorithm
Goal: decide whether positive integer n is perfect via proper-divisor sum.
Initialize sum = 0
Running total of proper divisors.
Loop i = 1 .. floor(n / 2)
If n % i === 0, add i to sum.
Compare sum and n
sum === n means perfect.
📜 Pseudocode
function properDivisorSum(n):
sum = 0
for i from 1 to floor(n / 2):
if n mod i == 0:
sum += i
return sum
function isPerfect(n):
if n < 2: return false
return properDivisorSum(n) == nCheck one number
Classic helper-based solution with 28 as sample.
function isPerfectNumber(number) {
let sum = 0;
for (let i = 1; i <= Math.floor(number / 2); i++) {
if (number % i === 0) {
sum += i;
}
}
return sum === number;
}
const number = 28;
if (isPerfectNumber(number)) {
console.log(number + " is a perfect number.");
} else {
console.log(number + " is not a perfect number.");
}Perfect numbers in a range
Reuse same helper over a small interval, e.g. 1 to 50.
function isPerfectNumber(num) {
let sum = 0;
for (let i = 1; i <= Math.floor(num / 2); i++) {
if (num % i === 0) {
sum += i;
}
}
return sum === num;
}
const values = [];
for (let i = 1; i <= 50; i++) {
if (isPerfectNumber(i)) {
values.push(String(i));
}
}
console.log("Perfect Numbers in the range 1 to 50:");
console.log(values.join(" "));Notes
Faster scan. You can iterate to Math.floor(Math.sqrt(n)) and add divisor pairs to reduce runtime.
Large numbers. JavaScript uses floating-point numbers; very large integers may lose exact precision.
Rarity. Perfect numbers are sparse, so scanning huge ranges is expensive.
❓ FAQ
🔄 Input / output examples
| Value tested | Typical output |
|---|---|
| 28 | 28 is a perfect number. |
| 10 | 10 is not a perfect number. |
| 6 | 6 is a perfect number. |
| 12 | 12 is not a perfect number. |
Edge cases
n = 1Not perfect
Proper-divisor sum is 0, so it does not match 1.
Always deficient
Only proper divisor is 1, so sum is less than the number.
Proper divisors only
Including n itself breaks the definition.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
One check (loop to n/2) | O(n) | O(1) |
| One check (sqrt pairing) | O(sqrt(n)) | O(1) |
Scan 1..U naively | O(U^2) | O(1) |
Summary
- Definition: perfect when proper-divisor sum equals the number.
- Pattern: loop with
%, accumulatesum, compare withn. - Care: handle
n = 1, large ranges, and precision for huge values.
The first four perfect numbers are 6, 28, 496, and 8128.
9 people found this page helpful
