Check a Perfect Number in Python
28 = 1 + 2 + 4 + 7 + 14. Find all proper divisors and check whether they add up to the number.
- Proper divisors
- Summing divisors with a comprehension
- Faster checks up to √n
Add the divisors except the number itself
The problem
Perfect numbers have fascinated mathematicians for over two thousand years, and they make a good exercise in finding divisors. Checking every number up to n works, but a small observation halves the work.
Think of it like this
Finding proper divisors is like checking which shorter planks can be laid end to end to exactly fill a shelf of length 28 without cutting. Planks of 1, 2, 4, 7 and 14 fit exactly. No plank longer than half the shelf can fit more than once, so you never need to try them.
range(1, number // 2 + 1)📏 Try planks up to half the shelfnumber % value == 0✅ This plank fits exactlydivisors🧰 The fitting planks: 1, 2, 4, 7, 14sum(divisors) == number⚖️ Together they measure 28: perfectA perfect number equals the sum of its proper divisors, the positive divisors smaller than the number itself, such as 28 = 1 + 2 + 4 + 7 + 14.
Key points
range(1, number // 2 + 1)stops at half the number, because no proper divisor is larger.number % value == 0keeps only the values that divide 28 exactly.divisorsholds[1, 2, 4, 7, 14].sum(divisors) == numberis True, soresultis"is".
When should you use it?
- Practicing divisor loops and list comprehensions.
- Number theory exercises: perfect, abundant and deficient numbers.
- Learning to cut a loop's range with a simple mathematical argument.
When not to use it
- Large numbers: looping to n / 2 is slow; divisors come in pairs, so checking up to the square root is enough.
- Searching for many perfect numbers: they are rare (6, 28, 496, 8128), so known formulas are better.
Example code
number = 28
# Proper divisors cannot be larger than half the number.
divisors = [value for value in range(1, number // 2 + 1) if number % value == 0]
result = "is" if sum(divisors) == number else "is not"
print(f"{number} {result} a perfect number")
Result of running the code
28 is a perfect number
Code explanation
The list comprehension tries every value from 1 to number // 2, because a proper divisor can never be larger than half the number. It keeps the values that leave no remainder, which for 28 are 1, 2, 4, 7 and 14. Their sum is 28, so the number is perfect.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Including the number itself
Every number divides itself, so the sum would always be too big.
divisors = [value for value in range(1, number + 1) if number % value == 0]
divisors = [value for value in range(1, number // 2 + 1) if number % value == 0]
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Classify a Number by Its Divisors Classify a positive integer as perfect, abundant, or deficient. Easy