Table of contents
1.
Introduction
2.
Understanding Perfect Numbers
3.
Why Learn About Perfect Numbers?
4.
Checking for a Perfect Number
5.
Writing a Python Program to Check for a Perfect Number
5.1.
Algorithm
5.2.
Python
5.3.
Explanation of the Code
6.
Optimizing the Perfect Number Check
6.1.
Python
6.2.
Explanation of Optimized Code
7.
Handling Large Numbers
7.1.
Advanced Optimization
7.2.
Python
7.3.
Explanation of Advanced Optimization
8.
Frequently Asked Questions
8.1.
What are the first few perfect numbers?
8.2.
Can a perfect number be odd?
8.3.
Are there infinitely many perfect numbers?
8.4.
How are perfect numbers and Mersenne primes related?
9.
Conclusion
Last Updated: Aug 28, 2025
Easy

Perfect Number in Python

Author Gaurav Gandhi
0 upvote

Introduction

A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. For instance, the number 6 is a perfect number because its divisors (excluding 6 itself) are 1, 2, and 3, and their sum is 6. This concept has been studied in number theory for centuries and has interesting properties. 

Perfect Number in Python

In this article, we'll learn how to check for perfect number in Python with easy-to-understand explanations and examples.

Understanding Perfect Numbers

A perfect number, nnn, is a number that satisfies the following condition:

sum of divisors of n−n=n\text{sum of divisors of } n - n = nsum of divisors of n−n=n

In other words, the sum of all divisors of nnn, excluding nnn itself, equals nnn. Some examples of perfect numbers are 6, 28, and 496.

Why Learn About Perfect Numbers?

Understanding perfect numbers is important for students and aspiring computer scientists because it helps in learning about algorithms, loops, and number theory. Additionally, it provides a practical application of programming skills to solve mathematical problems.

Checking for a Perfect Number

To determine if a number is perfect, we need to:

  1. Find all divisors of the number, excluding the number itself.
     
  2. Sum these divisors.
     
  3. Check if the sum is equal to the original number.

Writing a Python Program to Check for a Perfect Number

Let's write a simple Python program to check if a given number is perfect.

Algorithm

  1. Initialize a variable to store the sum of divisors.
     
  2. Loop through numbers from 1 to n−1n-1n−1.
     
  3. Check if the current number is a divisor of nnn.
     
  4. If yes, add it to the sum of divisors.
     
  5. After the loop, check if the sum equals nnn.

Code Example

  • Python

Python

def is_perfect_number(n):

   sum_of_divisors = 0

   for i in range(1, n):

       if n % i == 0:

           sum_of_divisors += i

   return sum_of_divisors == n

# Test the function

number = 28

if is_perfect_number(number):

   print(f"{number} is a perfect number.")

else:

   print(f"{number} is not a perfect number.")
You can also try this code with Online Python Compiler
Run Code


Output:

28 is a perfect number.

Explanation of the Code

  1. Function Definition: We define a function is_perfect_number(n) that takes an integer nnn as input.
     
  2. Sum of Divisors: We initialize sum_of_divisors to 0. This variable will store the sum of all divisors of nnn.
     
  3. Loop: We loop through numbers from 1 to n−1n-1n−1. For each number iii:
    • We check if iii is a divisor of nnn using n % i == 0.
       
    • If true, we add iii to sum_of_divisors.
       
  4. Return Result: After the loop, we return True if sum_of_divisors equals nnn; otherwise, we return False.

Optimizing the Perfect Number Check

To make the algorithm more efficient, we can loop only up to n/2n/2n/2, as a number cannot have a divisor greater than its half.

Optimized Code

  • Python

Python

def is_perfect_number(n):

   sum_of_divisors = 1

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

       if n % i == 0:

           sum_of_divisors += i

   return sum_of_divisors == n

# Test the function

number = 496

if is_perfect_number(number):

   print(f"{number} is a perfect number.")

else:

   print(f"{number} is not a perfect number.")
You can also try this code with Online Python Compiler
Run Code


Output

496 is a perfect number.

Explanation of Optimized Code

  1. Sum of Divisors: We start sum_of_divisors with 1, since 1 is a divisor of all numbers.
     
  2. Loop: We loop from 2 to n/2n/2n/2. This reduces the number of iterations, making the code more efficient.

Handling Large Numbers

For very large numbers, the above approach may still be inefficient. Using advanced algorithms and optimizations, such as checking for divisibility only up to the square root of nnn, can help.

Advanced Optimization

  • Python

Python

import math

def is_perfect_number(n):

   sum_of_divisors = 1

   for i in range(2, int(math.sqrt(n)) + 1):

       if n % i == 0:

           sum_of_divisors += i

           if i != n // i:

               sum_of_divisors += n // i

   return sum_of_divisors == n and n != 1

# Test the function

number = 8128

if is_perfect_number(number):

   print(f"{number} is a perfect number.")

else:

   print(f"{number} is not a perfect number.")
You can also try this code with Online Python Compiler
Run Code


Output

8128 is a perfect number.

Explanation of Advanced Optimization

  1. Sum of Divisors: Initialize sum_of_divisors with 1.
     
  2. Loop: Loop from 2 to the square root of nnn.
    • If iii is a divisor, add both iii and n/in/in/i to the sum.
       
    • Ensure that divisors are not counted twice.

Frequently Asked Questions

What are the first few perfect numbers?

The first few perfect numbers are 6, 28, 496, and 8128.

Can a perfect number be odd?

No odd perfect number has been discovered. All known perfect numbers are even.

Are there infinitely many perfect numbers?

It is unknown if there are infinitely many perfect numbers. The search continues.

How are perfect numbers and Mersenne primes related?

Every even perfect number is related to a Mersenne prime (a prime of the form 2p−12^p - 12p−1).

Conclusion

Perfect numbers are a fascinating concept in number theory with interesting properties and applications. By learning how to check for perfect numbers in Python, you can enhance your problem-solving and programming skills. With the provided algorithms and optimizations, you can efficiently identify perfect numbers and explore their properties further.

Recommended Readings:

Live masterclass