Perfect Number (LeetCode 507): Solution and Explanation

Easy
Math

Decide whether num equals the sum of its proper divisors. Divisors come in pairs around the square root, so one short scan finds them all.

Intuition

Testing every number from 1 to num - 1 would take up to 10^8 steps, which is far too slow.

If i divides num, then num / i divides it too. In each such pair one number is at most sqrt(num) and the other is at least sqrt(num).

So it is enough to try i from 2 up to sqrt(num) and add both members of every pair found. 1 is always a divisor, and num itself is never counted.

Approach: Divisor pairs up to the square root

  1. If num <= 1, return false: 1 has no proper divisors that sum to 1.
  2. Start total = 1, since 1 divides everything.
  3. Loop i from 2 while i * i <= num.
  4. When num % i == 0, add i, and also add num / i unless it equals i (a perfect square root).
  5. Return total == num.

Step-by-step example

num = 28
  1. 28 > 1, so total starts at 1. sqrt(28) is about 5.29, so i runs from 2 to 5.
  2. i = 2 divides 28. Add 2 and its partner 14: total = 17.
  3. i = 3 does not divide 28.
  4. i = 4 divides 28. Add 4 and its partner 7: total = 28.
  5. i = 5 does not divide 28. i = 6 has 36 > 28, so the loop ends. total = 28 equals num, so return true.

Code: Python, Java, C++

1class Solution:
2    def checkPerfectNumber(self, num: int) -> bool:
3        if num <= 1:
4            return False
5        total = 1
6        i = 2
7        while i * i <= num:
8            if num % i == 0:
9                total += i
10                if i * i != num:
11                    total += num // i
12            i += 1
13        return total == num
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(sqrt(num))

The loop tries at most sqrt(num) candidates, about 10,000 for num = 10^8.

Space

O(1)

Only the running total and the loop counter.

Other approaches

Check every number below num (time O(num), space O(1))

Add every i in 1..num-1 that divides num. Simple, but up to 10^8 iterations.

Euclid-Euler theorem (time O(1), space O(1))

Every even perfect number has the form 2^(p-1) * (2^p - 1) with 2^p - 1 prime, and no odd perfect number is known. Below 10^8 there are only five: 6, 28, 496, 8128 and 33550336, so a lookup works, but it explains nothing about divisors.

Common mistakes

  • Returning true for num = 1. Its proper divisor sum is 0, not 1, which is why the code handles it before starting total at 1.
  • Adding the square root twice for perfect squares such as 16, where i = 4 is its own partner.
  • Including num itself. Starting total at 1 and pairing from i = 2 never adds num.
  • Looping to num / 2 instead of sqrt(num). It is correct but about 5 * 10^7 steps at the upper limit.

Frequently asked questions

Why stop at the square root?

If both members of a divisor pair were larger than sqrt(num), their product would exceed num. So every pair has a member at or below the square root, and the scan finds it there.

Can total overflow a 32-bit integer?

No. The sum of divisors of any number up to 10^8 stays well under 2^31, so int is safe in Java, C++, C# and Go.

What are abundant and deficient numbers?

If the proper divisors sum to more than num (like 12, whose divisors sum to 16) the number is abundant. If they sum to less (like 7) it is deficient. Perfect numbers sit exactly between.