Write a Program to Find a Perfect Number in Python

Recently, I was working on a small project where I needed to check if a number was a perfect number.

At first, I thought Python might have a built-in function for this. But just like Excel doesn’t have a direct way to filter strikethrough cells, Python doesn’t come with a ready-made tool for perfect numbers. So, I had to write my own program.

In this tutorial, I’ll show you how I solved it. I’ll walk you through three simple methods: using loops, functions, and recursion.

Let’s dive in!

What is a Perfect Number?

A perfect number is a positive integer that equals the sum of its proper divisors (excluding itself).

For example:

  • 6 → Divisors are 1, 2, 3 → 1 + 2 + 3 = 6
  • 28Divisors are 1, 2, 4, 7, 14 → 1 + 2 + 4 + 7 + 14 = 28

Method 1 – Program to Find a Perfect Number Using Loops

When I first tried this, I wrote a simple loop to check all divisors of a number and add them up.

# Program to check if a number is perfect using loops

num = 28  # You can change this number

divisor_sum = 0

# Loop through numbers less than num
for i in range(1, num):
    if num % i == 0:
        divisor_sum += i

if divisor_sum == num:
    print(f"{num} is a Perfect Number")
else:
    print(f"{num} is NOT a Perfect Number")

You can see the output in the screenshot below.

perfect number in python

If you run this with num = 28, you’ll see that it prints: 28 is a Perfect Number.

Method 2 – Program to Find a Perfect Number Using a Function

Since I often reuse this logic, I wrote it as a function. This way, I can check multiple numbers quickly.

# Function to check perfect number

def is_perfect(num):
    divisor_sum = 0
    for i in range(1, num):
        if num % i == 0:
            divisor_sum += i
    return divisor_sum == num

# Test with a few numbers
for n in [6, 28, 12, 496]:
    if is_perfect(n):
        print(f"{n} is a Perfect Number")
    else:
        print(f"{n} is NOT a Perfect Number")

Output:

6 is a Perfect Number
28 is a Perfect Number
12 is NOT a Perfect Number
496 is a Perfect Number

You can see the output in the screenshot below.

perfect number

This method is cleaner and reusable.

Method 3 – Program to Find a Perfect Number Using Recursion

Sometimes, I like experimenting with recursion. Here’s how I rewrote the same logic in a recursive style.

# Recursive function to check perfect number

def sum_of_divisors(num, i=1):
    if i == num:
        return 0
    if num % i == 0:
        return i + sum_of_divisors(num, i + 1)
    else:
        return sum_of_divisors(num, i + 1)

def is_perfect(num):
    return sum_of_divisors(num) == num

# Example
num = 28
if is_perfect(num):
    print(f"{num} is a Perfect Number")
else:
    print(f"{num} is NOT a Perfect Number")

You can see the output in the screenshot below.

perfect numbers

Running this num = 28 gives 28 is a Perfect Number.

Check Perfect Numbers in a Range

In real projects, I often need to check more than one number. For example, let’s say I want all perfect numbers between 1 and 1000.

Here’s how I did it:

# Find all perfect numbers in a range

def is_perfect(num):
    divisor_sum = 0
    for i in range(1, num):
        if num % i == 0:
            divisor_sum += i
    return divisor_sum == num

# Check numbers from 1 to 1000
for n in range(1, 1001):
    if is_perfect(n):
        print(n)

Output:

6
28
496

These are the three perfect numbers under 1000.

Real-Life Example

I once used this logic in a small coding workshop for high school students in Texas. The students loved the concept of “perfect” numbers because it felt like a puzzle.

They even extended the logic to check for abundant and deficient numbers. That’s the beauty of Python: you can take a simple concept and expand it into a bigger project.

When I started, I didn’t realize how easy it would be to check for perfect numbers in Python.

The loop method is simple, the function method is reusable, and the recursion method is just fun to experiment with.

If you’re practicing Python, try all three methods. You’ll not only learn about perfect numbers but also improve your problem-solving skills.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.