AlgoMaster Logo

Perfect Number Check

Last Updated: June 7, 2026

easy
5 min read

Understanding the Problem

A perfect number equals the sum of its proper divisors. The proper divisors are all positive numbers that divide it evenly, except the number itself. For 6, the divisors are 1, 2, 3, and 6. Dropping 6 leaves the proper divisors 1, 2, and 3, and they add up to 6, so 6 is perfect. For 28, the proper divisors are 1, 2, 4, 7, and 14, and 1 + 2 + 4 + 7 + 14 = 28, so 28 is perfect as well.

A number that is not perfect fails this test in one of two ways. The sum can exceed the number, as with 12, whose proper divisors 1, 2, 3, 4, and 6 sum to 16. Or the sum can fall short, as with 27, whose proper divisors 1, 3, and 9 sum to 13. The check only passes when the sum equals the number exactly.

The boundary cases need direct handling. A perfect number is a positive integer, so n of 1 or smaller is never perfect. The number 1 has no proper divisors, since its only positive divisor is itself. Zero and negative numbers return false immediately, without computing any sum.

Key Constraints:

  • n can be 1 or smaller → A perfect number must be a positive integer greater than 1. Return false for 1, 0, and any negative n before doing divisor work, since none of them can equal a sum of proper divisors.
  • n can be large → A linear scan over every value below n is correct but slow for large inputs. Pairing divisors around the square root reduces the work to O(sqrt n).
  • The number itself is excluded → When summing divisors, count 1 but never count n. A divisor pair built from the square root can accidentally include n or double-count the square root, so the pairing has to be written carefully.

Approach 1: Sum Divisors up to n-1

Intuition

Test every integer from 1 up to n - 1 and keep the ones that divide n evenly. Their sum, compared to n, answers the question. The range stops at n - 1 because n itself is not a proper divisor.

This examines every value below n, so the work grows linearly with the number. The non-positive and 1 cases are filtered out before the loop runs.

Algorithm

  1. If n is 1 or smaller, return false.
  2. Set a running sum to 0.
  3. For each i from 1 to n - 1, if i divides n evenly, add i to the sum.
  4. Return true if the sum equals n, otherwise false.

Example Walkthrough

Input:

6
n

The value 6 is greater than 1, so the loop runs over i from 1 to 5. The value 1 divides 6, so the sum becomes 1. The value 2 divides 6, so the sum becomes 3. The value 3 divides 6, so the sum becomes 6. The values 4 and 5 do not divide 6, so the sum stays at 6. After the loop, the sum is 6, which equals n, so the answer is true.

true
result

Code

Approach 2: Square-Root Divisor Pairs

Intuition

Divisors come in pairs. If i divides n, then n / i divides n as well, and the two multiply back to n. For 28, 2 pairs with 14, and 4 pairs with 7. The smaller member of every pair is at most the square root of n, and the larger is at least the square root. So scanning i only from 1 up to the square root of n discovers every divisor: each time i divides n, record both i and n / i.

Two details keep the sum correct. First, the sum must count 1 but exclude n, since n is not a proper divisor. The pair built from i = 1 is 1 and n, so the larger member must not be added. Starting the sum at 1 and beginning the loop at i = 2 records the 1 without pulling in n. Second, when n is a perfect square, i and n / i are equal at the square root, and adding both would double-count it. Add the paired divisor only when i and n / i differ. This brings the work down from linear to O(sqrt n).

Algorithm

  1. If n is 1 or smaller, return false.
  2. Start the sum at 1, which counts the divisor 1 and reserves the exclusion of n itself.
  3. For each i from 2 while i * i is at most n, if i divides n:
    • Add i to the sum.
    • Compute the paired divisor n / i. If it differs from i, add it too.
  4. Return true if the sum equals n, otherwise false.

Example Walkthrough

Input:

28
n

The value 28 is greater than 1, so the sum starts at 1, accounting for the divisor 1 while leaving out 28 itself. The loop runs i from 2 while i * i is at most 28, which means i goes up to 5. At i = 2, 2 divides 28, so 2 is added and its pair 28 / 2 = 14 is added, bringing the sum to 1 + 2 + 14 = 17. At i = 3, 3 does not divide 28, so nothing changes. At i = 4, 4 divides 28, so 4 is added and its pair 28 / 4 = 7 is added, bringing the sum to 17 + 4 + 7 = 28. At i = 5, 5 does not divide 28. Since 6 * 6 = 36 is greater than 28, the loop stops. The final sum is 28, which equals n, so the answer is true.

true
result

Code