A positive integer is perfect when it equals the sum of its proper divisors — all positive divisors except the number itself.
Remember
n is perfect ↔ sum(proper divisors of n) == n
Example: 28
Proper divisors: 1, 2, 4, 7, 14
Sum = 1 + 2 + 4 + 7 + 14 = 28 ← perfect
n
Proper-divisor sum
Result
6
1 + 2 + 3 = 6
Perfect
28
1 + 2 + 4 + 7 + 14 = 28
Perfect
12
1 + 2 + 3 + 4 + 6 = 16
Abundant (not perfect)
10
1 + 2 + 5 = 8
Deficient (not perfect)
Approach
How to Check a Perfect Number in JavaScript
Four clear steps — then pick a simple scan or an O(√n) divisor pairing.
Require n > 1 (1 is not perfect).
Find every divisor of n that is strictly less than n.
Add those divisors together.
Compare the sum with n.
Method
Idea
Time
Best for
Simple loop
Test 1 … n-1 (or to n/2)
O(n)
Interviews, first explanation
√n pairing
Add i and n/i when they divide
O(√n)
Faster checks, larger n
Range scan
Reuse the helper for each value
O(N²) basic
Listing perfect numbers
Pseudocode
Pseudocode
function IsPerfect(n):
if n <= 1: return false
sum = 0
for i from 1 to n - 1:
if n mod i == 0:
sum = sum + i
return sum == n
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Proper divisors
Positive divisors excluding n
Test
sum === n
1
Not perfect
Class family
Deficient / perfect / abundant
First four
6, 28, 496, 8128
√n tip
Add both i and n/i; never add n
Try it
Live Preview
Enter a positive integer to see its proper divisors, their sum, and the verdict.
Positive integers up to 100000. Tap a chip or type a value.
Result
n = 28
Proper divisors: 1, 2, 4, 7, 14
Sum = 28
28 is a perfect number
Code
JavaScript Programs
Start with the simple loop so every proper divisor is obvious. Then list perfect numbers in a range, and finish with the √n method. Each example has a Try it Yourself editor.
Example 1 — Simple Loop
Sum every divisor from 1 to n / 2, then compare with n.
JavaScript
function isPerfect(n) {
if (n <= 1) {
return false;
}
let sum = 0;
for (let i = 1; i <= Math.floor(n / 2); i++) {
if (n % i === 0) {
sum += i;
}
}
return sum === n;
}
const number = 28;
if (isPerfect(number)) {
console.log(number + " is perfect");
} else {
console.log(number + " is not perfect");
}
Loop i from 1 to n / 2 so n itself is never added.
When n % i === 0, i is a proper divisor — add it to sum.
After the loop, compare sum === n.
For 28, the sum is 28, so the function returns true.
Example 2 — Perfect Numbers from 1 to 50
Reuse the helper and print every match on one line.
JavaScript
function isPerfect(n) {
if (n <= 1) {
return false;
}
let sum = 0;
for (let i = 1; i <= Math.floor(n / 2); i++) {
if (n % i === 0) {
sum += i;
}
}
return sum === n;
}
console.log("Perfect numbers from 1 to 50:");
let line = "";
for (let i = 1; i <= 50; i++) {
if (isPerfect(i)) {
line += i + " ";
}
}
console.log(line.trim());
Each value is tested with the same isPerfect helper.
Matches are collected into one space-separated line.
Only 6 and 28 appear in this band.
Keeping the helper pure makes range listing easy.
Example 3 — √n Pairing
Walk only up to √n and add both halves of each divisor pair, skipping n.
JavaScript
function isPerfect(n) {
if (n <= 1) {
return false;
}
let sum = 1; // 1 is always a proper divisor for n > 1
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
sum += i;
const pair = n / i;
if (pair !== i && pair !== n) {
sum += pair;
}
}
}
return sum === n;
}
console.log(isPerfect(6)); // true
console.log(isPerfect(28)); // true
console.log(isPerfect(12)); // false
console.log(isPerfect(10)); // false