0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Perfect Number

A perfect number is a number equal to the sum of its positive divisors excluding itself; in JavaScript, you can check it using a loop to sum divisors and compare with the number, like function isPerfect(num) { let sum = 0; for (let i = 1; i <= num / 2; i++) { if (num % i === 0) sum += i; } return sum === num; }.
📋

Examples

Input6
Output6 is a perfect number
Input28
Output28 is a perfect number
Input10
Output10 is not a perfect number
🧠

How to Think About It

To find if a number is perfect, think about all the numbers smaller than it that divide it evenly (no remainder). Add those numbers up. If the total equals the original number, it is perfect; otherwise, it is not.
📐

Algorithm

1
Get the input number.
2
Initialize a sum to zero.
3
Loop from 1 to one less than the number.
4
If the current number divides the input number evenly, add it to the sum.
5
After the loop, compare the sum with the input number.
6
Return true if they are equal, otherwise false.
💻

Code

javascript
function isPerfect(num) {
  let sum = 0;
  for (let i = 1; i <= num / 2; i++) {
    if (num % i === 0) {
      sum += i;
    }
  }
  return sum === num;
}

const number = 28;
if (isPerfect(number)) {
  console.log(number + ' is a perfect number');
} else {
  console.log(number + ' is not a perfect number');
}
Output
28 is a perfect number
🔍

Dry Run

Let's trace the number 6 through the code to see how it checks if 6 is perfect.

1

Initialize sum

sum = 0

2

Check divisors from 1 to 3

i = 1 to 3

3

Add divisors that divide 6 evenly

1 divides 6, sum = 0 + 1 = 1; 2 divides 6, sum = 1 + 2 = 3; 3 divides 6, sum = 3 + 3 = 6

4

Compare sum with number

sum = 6, number = 6, sum === number is true

inum % isum
16 % 1 = 01
26 % 2 = 03
36 % 3 = 06
46 % 4 = 26
56 % 5 = 16
💡

Why This Works

Step 1: Find divisors

The code finds all numbers less than the input that divide it evenly using num % i === 0.

Step 2: Sum divisors

It adds these divisors to a sum to get the total of all proper divisors.

Step 3: Compare sum to number

If the sum equals the original number, the number is perfect, so the function returns true.

🔄

Alternative Approaches

Check divisors only up to half the number
javascript
function isPerfect(num) {
  let sum = 0;
  for (let i = 1; i <= num / 2; i++) {
    if (num % i === 0) sum += i;
  }
  return sum === num;
}

console.log(isPerfect(28) ? '28 is a perfect number' : '28 is not a perfect number');
This reduces the number of checks because no divisor can be greater than half the number, improving efficiency.
Use recursion to sum divisors
javascript
function sumDivisors(num, i = 1) {
  if (i > num / 2) return 0;
  return (num % i === 0 ? i : 0) + sumDivisors(num, i + 1);
}

function isPerfect(num) {
  return sumDivisors(num) === num;
}

console.log(isPerfect(6) ? '6 is a perfect number' : '6 is not a perfect number');
This uses recursion instead of a loop, which is elegant but less efficient and can cause stack overflow for large numbers.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n-1 (or n/2 in the optimized version), so it takes linear time proportional to the input number.

Space Complexity

The program uses a fixed amount of extra space for variables, so space complexity is constant.

Which Approach is Fastest?

Checking divisors only up to half the number is faster than checking all numbers less than n. Recursion is less efficient and uses more stack space.

ApproachTimeSpaceBest For
Check all divisors up to n-1O(n)O(1)Simple understanding
Check divisors up to n/2O(n/2) ~ O(n)O(1)Better performance
Recursive divisor sumO(n)O(n)Elegant but less efficient
💡
Check divisors only up to half the number to save time.
⚠️
Including the number itself in the sum of divisors, which makes every number incorrectly perfect.