Check Perfect Number in Python

Beginner
⏱️ 11 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What perfect, deficient, and abundant numbers mean.
  • How to sum proper divisors in Python.
  • How to check one number and scan a range.

Overview

A perfect number equals the sum of its proper divisors. For example, 6 is perfect because 1 + 2 + 3 = 6.

Two programs

One single-value check and one range scan.

Live preview

See divisors, sum, and classification immediately.

Interview-ready flow

Clear loop, edge case for 1, and complexity discussion.

Prerequisites

Python loops, modulo operator, and basic function syntax.

  • Use for i in range(...) loops comfortably.
  • Know that n % i == 0 means i divides n.

What is a perfect number?

A positive integer n is perfect when the sum of its proper divisors equals n exactly.

Using s(n) = sum of proper divisors:

Deficient s(n) < n
Perfect s(n) = n
Abundant s(n) > n

Equivalent formulas

If sigma(n) is the sum of all positive divisors, then s(n) = sigma(n) - n. So s(n) = n is equivalent to sigma(n) = 2n.

Checklist

Compute proper divisor sum and compare with n.

Quick examples

6Perfect
Divisors
1, 2, 3
Sum
6
28Perfect
Divisors
1, 2, 4, 7, 14
Sum
28
10Deficient
Divisors
1, 2, 5
Sum
8
12Abundant
Divisors
1, 2, 3, 4, 6
Sum
16

Live preview

Enter a positive integer and inspect proper divisors, sum, and verdict.

Use whole numbers n >= 1.

Live result
Press "Run check" to see the result.

Algorithm

Goal: check if proper divisor sum equals n.

Initialize sum

Start with sum = 0.

Scan divisors

For i from 1 to n // 2, if n % i == 0 then add i.

Compare

If sum == n, number is perfect.

📜 Pseudocode

Pseudocode
function proper_divisor_sum(n):
    sum = 0
    for i from 1 to floor(n / 2):
        if n mod i == 0:
            sum = sum + i
    return sum

function is_perfect(n):
    if n < 2:
        return false
    return proper_divisor_sum(n) == n
1

Check one number

Test a fixed value (28) with a helper function.

python
def is_perfect_number(number: int) -> bool:
    total = 0
    for i in range(1, number // 2 + 1):
        if number % i == 0:
            total += i
    return total == number


number = 28
if is_perfect_number(number):
    print(f"{number} is a perfect number.")
else:
    print(f"{number} is not a perfect number.")
2

Perfect numbers in range 1 to 50

Reuse helper and print all perfect numbers in a small interval.

python
def is_perfect_number(num: int) -> bool:
    total = 0
    for i in range(1, num // 2 + 1):
        if num % i == 0:
            total += i
    return total == num


print("Perfect Numbers in the range 1 to 50:")
for i in range(1, 51):
    if is_perfect_number(i):
        print(i, end=" ")

Notes

Faster check. You can test divisors only up to sqrt(n) and add divisor pairs.

Rarity. Perfect numbers are rare, so broad brute-force scans are expensive.

❓ FAQ

A positive integer is perfect if the sum of its proper divisors equals the number itself.
No. Its proper divisor sum is treated as 0, not 1.
If proper divisor sum is less than n it is deficient, if greater than n it is abundant.
No proper divisor of n can be larger than n // 2.
Yes. It is mathematically equivalent to proper-divisor sum equals n.
No, they are very rare.

🔄 Input / output examples

Replace fixed values in code or read from input for interactive runs.

ValueOutput idea
28Perfect number
10Not perfect
6Perfect number
12Not perfect

Edge cases

n = 1

Not perfect

Proper divisor sum is 0, not 1.

Prime numbers

Always deficient

Only proper divisor is 1, so sum is less than n.

Do not include n

Proper divisors only

Adding n itself breaks the definition.

⏱️ Time and space complexity

TaskTimeExtra space
Single check with n // 2 scanO(n)O(1)
Single check with sqrt(n) pairingO(sqrt(n))O(1)
Range scan 1..U (naive)O(U^2)O(1)

Summary

  • Definition: perfect means proper divisor sum equals n.
  • Loop: check divisors from 1 to n // 2 with modulo.
  • Edge case: 1 is not perfect.
Did you know?

The first four perfect numbers are 6, 28, 496, and 8128. Whether any odd perfect number exists is still unknown.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful