A Perfect Number is a positive integer equal to the sum of all its positive divisors, excluding the number itself.
Let’s say the input number is 6.
- The divisors of 6 are 1, 2, and 3.
- The sum of these divisors is 1 + 2 + 3 = 6.
- Since the sum is equal to the original number, 6 is a perfect number.
This article explores different ways to find perfect numbers in Python, with explanations and examples. We’ll delve into the logic behind finding divisors, summing them efficiently, and checking for perfect number properties.
This article explores different methods, including using the modulo operator, loops, list comprehensions, and math module.
Table of contents
1. How to Find a Perfect Number in Python
Below are the steps to find a perfect number in Python:
- Input
Set number or take an integer
numberas input from the user. - Initialization
Initialize a variable
divisors_sumto 0. This variable will store the sum of all proper divisors. - Find divisors using loop
Iterate through the numbers from 1 up to (but not including)
number. Inside the loop, for each number in this range, check if it is a divisor ofnumber(i.e., ifnumberis divisible by the current number without any remainder). If a number is a divisor, add it to the.divisors_sum - Check for Perfect Number
After iterating through all potential divisors, compare the
with the originaldivisors_sumnumber. If theis equal todivisors_sumnumber, thennumberis a perfect number. Otherwise, it is not a perfect number. - Output
Print a message indicating whether the input
numberis a perfect number or not.
Code Example
2. Using math.sqrt function
Now, Let’s see how to find a perfect number using a math module.
The math.sqrt() is a Python built-in function that calculates the square root of a given number. For example, math.sqrt(25) returns 5.0.
In this method, the modulo operator (%) is used to find the divisor of a number. Here, instead of checking all numbers up to n, you only need to check up to the square root of n (√n).
This method improves efficiency by only checking divisors up to the square root of the number, leveraging the fact that divisors occur in pairs.
For Example,
- For
number = 28:- The square root of 28 is approximately 5.29, so the loop checks divisors up to 5.
- Divisor pairs found:
- For
i = 2,number // i = 14. Add both 2 and 14 to the sum. - For
i = 4,number // i = 7. Add both 4 and 7 to the sum.
- For
- Total divisor sum:
1 + 2 + 14 + 4 + 7 = 28. - Since the sum equals the original number, 28 is confirmed as a perfect number.
Code Example
Output:
Is 28 a perfect number? -> TrueCode language: Python (python)
Explanation
- Initialization: The sum of divisors starts at 1 because 1 is a divisor of every number.
- Square Root Check: The loop runs only up to the square root of
num, calculated usingmath.sqrt(num). This reduces unnecessary checks for numbers larger than √n. - Divisor Check: For each number
iin the range, the modulo operator (%) checks ifiis a divisor ofnum. Ifnum % i == 0, it confirms thatidividesnumwithout leaving a remainder. - Divisor Pairs: Whenever a divisor
iis found, the corresponding divisornum // iis also calculated and added to the sum. This ensures all divisor pairs are included, except wheniequalsnum // i(to avoid duplication). - Comparison: After calculating the sum of divisors, it is compared with the original number. If they are equal, then it is a perfect number.
3. Using List Comprehension
List comprehension is a concise way to create Python lists by applying an expression to each item in an iterable, with optional filtering using a condition. It allows you to create a list of divisors efficiently.
The built-in sum() function is then used to calculate the total sum of the divisors in the list, enabling a concise and elegant way to determine if a number is perfect.
Code Example
Output:
Is 28 a perfect number? -> TrueCode language: Python (python)
Explanation
- List Comprehension: It generates a list of all divisors of num by iterating through numbers from 1 to
num-1and including only those that divide num with reminder 0, i.e.,num % i == 0. sum()function: It calculates the total sum of the divisors.- Comparison: After calculating the sum of divisors, it is compared with the original number. If they are equal, then it is a perfect number.
Summary
Each method has its unique advantages depending on the use case.
- For Loop:
- Advantage: Simple and easy to understand.
- Time Complexity: O(n) – The loop runs from 1 to
num-1, performing a constant-time check for each number. - Space Complexity: O(1) – Only a few variables are used to store the sum and current divisor.
- Using
math.sqrtfunction: Best Choice- Advantage: Fast method. Significantly reduces iterations by leveraging divisor pairs.
- Time Complexity: O(√n) – The loop runs up to the square root of
num. - Space Complexity: O(1) – Uses only a few variables for the sum and divisors.
- List Comprehension:
- Advantage: Concise and Pythonic; combines looping and filtering in a single line.
- Time Complexity: O(n) – Iterates through all numbers from 1 to
num-1to generate the list of divisors. - Space Complexity: O(n) – Creates a list of divisors, requiring additional memory to store the list.

Leave a Reply