**Perfect Number in python:**

If the sum of a number’s appropriate divisors (excluding the number itself) equals the number, the number is said to be the perfect number.

Consider the following example: appropriate divisors of 6 are 1, 2, 3. Because the sum of these divisors equals 6 (1+2+3=6), 6 is considered a perfect number. When we consider another number, such as 12, the proper divisors of 12 are 1, 2, 3, 4, and 6. Now, because the sum of these divisors does not equal 12, 12 is not a perfect number.

Python programming is simpler and more enjoyable than programming in other languages due to its simplified syntax and superior readability. Now that we understand the concept of a perfect number, let’s construct a Python program to determine whether or not a number is a perfect number. Let’s write some Python code to see if the given user input is a perfect number or not, and have some fun with Python coding.

**Examples:**

**Example1:**

**Input:**

given number = 6

**Output:**

The given number 6 is perfect number

**Example2:**

**Input:**

given number = 179

**Output:**

The given number 179 is not a perfect number

## Python Program to Check Whether the given Number is Perfect Number or Not

There are several ways to check whether the given number is perfect number or not some of them are:

- Iterating from 2 to N-1
- Iterating from 2 to N/2
- Efficient Approach (Iterating till Square root of N)

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

### Method #1: Iterating from 2 to N-1

A simple technique for determining a perfect number is to loop from 2 to given_number-1, keep track of the sum of the number’s proper divisors, and see if the sum equals the number.

**Below is the implementation:**

# python program to cheeck whether the given numner is perfect number or not # function which returns true if the given number is # perfect number else it will return False def checkPerfectNumb(givenNumb): # Taking a variable totalSum and initializing it with 1 totalSum = 1 # Iterating from 2 to n-1 for i in range(2, givenNumb): # if the iterator value is divides the number then add the given number to totalSum if givenNumb % i == 0: totalSum += i # if the totalSum is equal to the given number # then it is perfect number else it is not perfect number if(totalSum == givenNumb): # if it is true then it is perfect number then return true return True # if nothing is returned then it is not a perfect number so return False return False # Given number given_numb = 6 # passing the givennumber to checkPerfectNumb to check whether it is perfect number or not if(checkPerfectNumb(given_numb)): print("The given number", given_numb, "is perfect number") else: print("The given number", given_numb, "is not a perfect number")

**Output:**

The given number 6 is perfect number

We must determine whether n is a perfect number. It is worth noting that we are initializing the **totalSum** with 1 because 1 is a valid divisor for all integers (except zero), allowing us to skip an iteration in the loop and start from 2.

If it is a proper divisor, we loop over 2 to number-1 and add the integers to **totalSum**. Finally, when we exit the loop, we check to see if the sum obtained is equal to the number.

It requires **O(n) Time Complexity.**

### Method #2:Iterating from 2 to N/2

After running the preceding program through its tests, we may think it would be possible to optimize it. But, without modifying the technique, we can reduce the number of iterations to number/2. We came to the conclusion that a number cannot have a suitable divisor bigger than number/2.

**Below is the implementation:**

# python program to cheeck whether the given numner is perfect number or not # function which returns true if the given number is # perfect number else it will return False def checkPerfectNumb(givenNumb): # Taking a variable totalSum and initializing it with 1 totalSum = 1 # Iterating from 2 to given_number//2 +1 for i in range(2, givenNumb//2 + 1): # if the iterator value is divides the number then add the given number to totalSum if givenNumb % i == 0: totalSum += i # if the totalSum is equal to the given number # then it is perfect number else it is not perfect number if(totalSum == givenNumb): # if it is true then it is perfect number then return true return True # if nothing is returned then it is not a perfect number so return False return False # Given number given_numb = 6 # passing the givennumber to checkPerfectNumb to check whether it is perfect number or not if(checkPerfectNumb(given_numb)): print("The given number", given_numb, "is perfect number") else: print("The given number", given_numb, "is not a perfect number")

**Output:**

The given number 6 is perfect number

The preceding excerpt is nearly identical to the previous one, with the exception of looping till number/2. It’s worth noting that we’re doing an integer division instead of converting it to a float type, and we’re looping until n/2+1 because the last integer in the range is ignored by the Python loop.

It requires **O(n) Time Complexity.**

### Method #3: Efficient Approach (Iterating till Square root of N)

An Efficient Solution is to go through the numbers till you get to the square root of n. If a number i divides n, then sum both i and n/i.

**Below is the implementation:**

# python program to cheeck whether the given numner is perfect number or not # function which returns true if the given number is # perfect number else it will return False def checkPerfectNumb(givenNumb): # Taking a variable totalSum and initializing it with 1 totalSum = 1 k = 2 while k * k <= givenNumb: # if the iterator value is divides the number then add the given number to totalSum if givenNumb % k == 0: totalSum = totalSum + k + givenNumb/k k += 1 # if the totalSum is equal to the given number # then it is perfect number else it is not perfect number if(totalSum == givenNumb): # if it is true then it is perfect number then return true return True # if nothing is returned then it is not a perfect number so return False return False # Given number given_numb = 6 # passing the givennumber to checkPerfectNumb to check whether it is perfect number or not if(checkPerfectNumb(given_numb)): print("The given number", given_numb, "is perfect number") else: print("The given number", given_numb, "is not a perfect number")

**Output:**

The given number 6 is perfect number

This is the efficient approach to do the same problem quickly compared to first two methods.

It requires **O(Sqrt(n)) Time Complexity.**

**Related Programs**: