Perfect Number Program in Python – Explained with Examples

If you're learning Python and want to practice number-based logic, writing a perfect number program is a great place to start. It's a popular beginner-friendly problem that teaches you about loops, conditionals, and mathematical logic.

Perfect Number Program in Python

In this blog post, we'll explore:

  • 1. What is a perfect number?
     
  • 2. How to check if a number is perfect
     
  • 3. A step-by-step Python program
     
  • 4. Examples and edge cases
     
  • 5. How to write cleaner, optimized code
     
  • 6. Where you can learn such problems in depth (Hint: Uncodemy’s Python Programming Course)
     

Let’s begin!

✅ What is a Perfect Number?

perfect number is a positive integer that is equal to the sum of its proper positive divisors, excluding itself.

🔍 Definition:

A number n is perfect if:
 n = sum of all its divisors (excluding n itself)

🔢 Examples:

  • a. 6
       Divisors: 1, 2, 3
       Sum: 1 + 2 + 3 = 6 ✅ Perfect
     
  • b. 28
       Divisors: 1, 2, 4, 7, 14
       Sum: 1 + 2 + 4 + 7 + 14 = 28 ✅ Perfect
     
  • c. 12
       Divisors: 1, 2, 3, 4, 6
       Sum: 1 + 2 + 3 + 4 + 6 = 16 ❌ Not Perfect
     

Perfect numbers are rare and fascinating—they're tied to ancient mathematics and even cryptographic theories!

🧮 Perfect Number Logic in Python

To check whether a number is perfect, follow this logic:

1. Take a number n

2. Loop from 1 to n-1

3. For each number i, check if it divides n (i.e., n % i == 0)

4. Sum up all such i

5. If the sum equals n, then it’s perfect

💻 Python Program to Check Perfect Number

Let’s write the complete Python program step by step.

✅ Code:

python

CopyEdit

Copy Code

# Perfect Number Program in Python

def is_perfect(number):

    if number < 1:

        return False

    sum_of_divisors = 0

    for i in range(1, number):

        if number % i == 0:

            sum_of_divisors += i

    return sum_of_divisors == number

# Taking input from user

num = int(input("Enter a number: "))

if is_perfect(num):

    print(f"{num} is a Perfect Number ✅")

else:

    print(f"{num} is NOT a Perfect Number ❌")

🧾 Sample Output:

less

CopyEdit

Enter a number: 28

28 is a Perfect Number ✅

🧠 How It Works – Code Explained

Let’s break the logic:

  • 1. We define a function is_perfect()
     
  • 2. Loop through all numbers from 1 to n-1
     
  • 3. Check if the current number divides n without a remainder
     
  • 4. Add it to the sum_of_divisors
     
  • 5. Finally, compare the sum to the original number

🛠️ Bonus Tip:

You can reduce time complexity by looping only till n // 2. Why? No divisor (except n itself) can be more than half of n.

python

CopyEdit

for i in range(1, number // 2 + 1):

📈 Optimized Version of the Program

python

CopyEdit

Copy Code

def is_perfect(number):

    if number < 2:

        return False

    sum_of_divisors = 1  # Start with 1 as it's always a divisor

    for i in range(2, number // 2 + 1):

        if number % i == 0:

            sum_of_divisors += i

    return sum_of_divisors == number

This version improves performance slightly, especially for larger numbers.

🧪 Test Cases

InputOutput
66 is a Perfect Number ✅
2828 is a Perfect Number ✅
496496 is a Perfect Number ✅
1212 is NOT a Perfect Number ❌
00 is NOT a Perfect Number ❌
-10-10 is NOT a Perfect Number ❌

❗ Edge Cases to Consider

  • 1. Negative numbers ❌
     
  • 2. 0 or 1 ❌
     
  • 3. Very large numbers (optimization needed) ⚙️
     

Perfect numbers are rare—only a few exist under 10,000.

🧮 List of Known Perfect Numbers (Below 10000)

1. 6

2. 28

3. 496

4. 8128

Want to print all perfect numbers in a given range? Let’s write a loop!

🔄 Find All Perfect Numbers in a Range

python

CopyEdit

Copy Code

def is_perfect(n):

    if n < 2:

        return False

    total = 1

    for i in range(2, n // 2 + 1):

        if n % i == 0:

            total += i

    return total == n

# Find perfect numbers from 1 to 10000

for num in range(1, 10001):

    if is_perfect(num):

        print(num, "is a Perfect Number")

Output:

csharp

CopyEdit

6 is a Perfect Number  

28 is a Perfect Number  

496 is a Perfect Number  

8128 is a Perfect Number

🎓 Learn Python Perfectly with Uncodemy

Solving problems like perfect number programs is an important part of Python learning. But to truly build confidence, you need more structured learning, regular practice, and expert guidance.

That’s why we recommend the Python Programming Course by Uncodemy.

🚀 Why Choose Uncodemy?

✅ Beginner to Advanced Python Modules
✅ Hands-on Projects and Assignments
✅ Live Sessions + Mentor Support
✅ Coding Challenges & Interview Preparation
✅ Affordable and Flexible Learning
✅ 100% Job Assistance

Whether you're new to programming or looking to ace Python interviews, Uncodemy provides everything you need—from logic building to real-world applications.

👉 Enroll in Python Course by Uncodemy and start solving smarter.

📝 Final Thoughts

The Perfect Number Program in Python is more than a coding challenge—it's a practical way to learn:

  • 1. Divisors and modulus operator
     
  • 2. Looping and conditional logic
     
  • 3. Functions and return values
     
  • 4. Optimizing code with math
     

Now that you know how to write and optimize a perfect number checker, take this further—modify it, enhance it, and most importantly, keep experimenting.

And when you’re ready to master Python beyond the basics, Uncodemy’s expert-led course is just a click away.

Happy Coding! 🐍

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses