Check Perfect Number in JavaScript

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 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 === 0 means i divides n evenly.

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:

Deficients(n) < n
Perfects(n) = n
Abundants(n) > n

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

6Perfect
Proper divisors
1, 2, 3
Sum
6
28Perfect
Proper divisors
1, 2, 4, 7, 14
Sum
28
10Deficient
Proper divisors
1, 2, 5
Sum
8
12Abundant
Proper divisors
1, 2, 3, 4, 6
Sum
16

Live preview

Walks from 1 to n/2, sums proper divisors, and classifies the result.

Use whole numbers n ≥ 1. Values above 999999 are blocked for responsiveness.

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

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

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

Check one number

Classic helper-based solution with 28 as sample.

JavaScript
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.");
}
Try it Yourself
2

Perfect numbers in a range

Reuse same helper over a small interval, e.g. 1 to 50.

JavaScript
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(" "));
Try it Yourself

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

A positive integer is perfect when the sum of all its proper divisors (excluding itself) equals the number. Example: 6 = 1 + 2 + 3.
No. Its proper-divisor sum is treated as 0, so it does not equal 1.
With s(n) as proper-divisor sum: s(n) > n is abundant, s(n) < n is deficient, and s(n) = n is perfect.
No proper divisor can exceed n/2, so the check is complete while staying simple for beginners.
Yes. You can loop to sqrt(n) and add divisor pairs, reducing time for one check from O(n) to O(sqrt(n)).
No, they are rare. That is why beginner examples usually test 28 or scan only a small range.

🔄 Input / output examples

Value testedTypical output
2828 is a perfect number.
1010 is not a perfect number.
66 is a perfect number.
1212 is not a perfect number.

Edge cases

n = 1

Not perfect

Proper-divisor sum is 0, so it does not match 1.

Prime numbers

Always deficient

Only proper divisor is 1, so sum is less than the number.

Do not add n

Proper divisors only

Including n itself breaks the definition.

⏱️ Time and space complexity

TaskTimeExtra space
One check (loop to n/2)O(n)O(1)
One check (sqrt pairing)O(sqrt(n))O(1)
Scan 1..U naivelyO(U^2)O(1)

Summary

  • Definition: perfect when proper-divisor sum equals the number.
  • Pattern: loop with %, accumulate sum, compare with n.
  • Care: handle n = 1, large ranges, and precision for huge values.
Did you know?

The first four perfect numbers are 6, 28, 496, and 8128.

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