Beginner Python Exercises

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.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Proper divisors
  • Summing divisors with a comprehension
  • Faster checks up to √n
Divisors of 28
1
2
4
7
14
= 28
1 + 2 + 4 + 7 + 14 = 28, so 28 is a perfect number.

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.

From shelf planks to code
range(1, number // 2 + 1)📏 Try planks up to half the shelf
number % value == 0✅ This plank fits exactly
divisors🧰 The fitting planks: 1, 2, 4, 7, 14
sum(divisors) == number⚖️ Together they measure 28: perfect
Proper divisors are never larger than half the number.
In one sentence

A 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

  1. range(1, number // 2 + 1) stops at half the number, because no proper divisor is larger.
  2. number % value == 0 keeps only the values that divide 28 exactly.
  3. divisors holds [1, 2, 4, 7, 14].
  4. sum(divisors) == number is True, so result is "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

Run code →
main.py
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")
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Including the number itself

Every number divides itself, so the sum would always be too big.

Problem
divisors = [value for value in range(1, number + 1) if number % value == 0]
Fix
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.

All challenges