# How to find perfect number in Python

Do you know how to work with a perfect number in Python? This Python tutorial will discuss how to find the perfect number in Python. And also we are going to cover the following given topics.

• How to find perfect number in Python
• How to find perfect number in Python using for loop
• How to find perfect number in Python using while loop
• How to find perfect number in Python using list
• How to find perfect square number in python
• How to find perfect number function in Python
• How to find python program for perfect numbers or not
• How to find python print all perfect numbers
• How to find perfect number in Python using recursion

## Perfect number in Python

• In Python, any number can be a perfect number if all of its positive divisors, excluding the number itself, add up to the same value.
• In simple words, a number is considered to be perfect if its sum of divisors equals the number.
• First, we will take input using an integer and store them in a variable. We will now define a variable named “result,” where we will store the total of the specified number’s divisors.
• The following task is to create a for loop in which we will divide our number by the initialized value of the variable and then increase the value of I and determine which numbers result in a remainder of zero.
• Now, using the variable “result,” we will add the number’s divisors.
• Finally, to compare the user-provided number with the value, we will utilize the decision statement keyword “if.”
• In Python, for instance, 6 is a perfect number because it can be divided by 1, 2, 3, and 6. Therefore, 1+2+3 = 6 (remember, we have to ignore the number itself.) is the sum of these values.

Example:

``````input_num = int(input("Enter the value:"))
result = 0
for m in range(1, input_num ):
if(input_num  % m == 0):
result = result + m
if (result == input_num ):
print("Yes it is a Perfect number!")
else:
print("No ,the value is not a Perfect number!")``````

Here is the Screenshot of the following given code

This is how we can get the perfect number in Python.

## Perfect number in Python using while loop

• In this section, we will discuss how to get the perfect number in Python by using a while loop in Python.
• To perform this particular task, we will use the while loop concept and in this example, we will use the input user and enter the number. Next, we will apply the while loop to generate numbers from 1 to n where n is not included as we need the sum of the proper divisors of the number.
• Until the specified condition is False, a block of statements will be repeated using the while loop in Python.
• In this example, we will set the condition while n<new_val then (new_val % n == 0) and if the number is perfect then it will display “it is a perfect number” otherwise it will return it is not a perfect number.

Example:

``````new_val = int(input("Enter the value: "))
n = 1
result = 0
while(n < new_val):
if(new_val  % n == 0):
result = result + n
n = n + 1
if (result == new_val):
print("It is a Perfect Number",new_val)
else:
print("It is not the Perfect Number",new_val)``````

Here is the implementation of the following given code

In this example, we have understood how to display the perfect number in Python by using the while loop.

## Perfect number in Python using for loop

• Here, we will discuss how to display the perfect number in Python by using the for-loop method in Python.
• The for loop in Python is commonly used to iterate through items that can be iterated over, such as lists, tuples, or strings.
• In this example First, we will take input using an integer and store them in a variable. We will now define a variable named “result,” where we will store the total of the specified number’s divisors.

Example:

``````input_num = int(input("Enter the value:"))
result = 0
for m in range(1, input_num ):
if(input_num  % m == 0):
result = result + m
if (result == input_num ):
print("Yes it is a Perfect number!")
else:
print("No ,the value is not a Perfect number!")``````

In the following given code, first, we will take the input from the user and set the condition (input_num % m == 0) if it is equal then it will display the yes it is a perfect number.

Here is the implementation of the following given code.

## perfect square number in python

• A perfect square is any number that can be represented as the product of two entire, equal numbers. For instance, the number 36 is a perfect square since it can be expressed as 6*6.
• Python includes a built-in sqrt() function that returns a number’s square root. It explains how to calculate the square root of a value that multiplies itself to get a number. Since the sqrt() method can’t be called directly to get the square root of a given number, we must use a math module.
• Let’s say we have n. We must determine whether or not the number n is a perfect square. When a number’s square root is an integer, it is considered to be a perfect square number.

Example:

``````import math

new_val = int(input("Enter the value: "))

result = math.sqrt(new_val)
if int(result + 0.5) ** 2 == new_val:
print(new_val, "Yes it is a perfect square")
else:
print(new_val, "No it is not a perfect square")``````

In the above code first, we imported the math module and then take the input user. Next, we used math.sqrt() function and within this function we passed the ‘new_val’ as an argument.

Here is the implementation of the following given code.

This is how we can create a perfect square number in Python by using math.sqrt() function.

## Perfect number function in Python

• A perfect number is a positive integer that, excluding the number itself, equals the total of its positive divisors. Take the number 6 as an example. Its divisors, excluding itself, are 1, 2, and 3. Hence, the sum of its divisors, i.e., 1+2+3, equals 6.
• In Python, a function can be used to determine whether a number is perfect or not. A unit of code known as a function carries out a particular task.
• Any number may be entered with this Perfect Number program. With the help of this number, the functions will determine whether the number is a perfect number or not.

Example:

``````def new_perf_num(m):
total = 0
for z in range(1,m):
if(m%z == 0):
total = total+z
m = 7

if(m == new_perf_num(m)):
print(m, "It is a perfect number")
else:
print(m, "It is not a perfect number") ``````

Here is the execution of the following given code.

## Python program for perfect numbers or not

• In this section, we will discuss how to check whether the number is perfect or not in Python.
• The following task is to create a for loop in which we will divide our number by the initialized value of the variable and then increase the value of I and determine which numbers result in a remainder of zero.
• First, we must ask the user for an integer, which will be saved in the field input number. Next, declare a variable called “sum variable” that will hold the input number’s divisors’ sum.
• Using a for loop, we will determine whether the input number is divided by the supplied number, or whether it results in zero reminders. These numbers will be our divisors. Now, add each divisor to the sum variable.
• The final step compares the user-provided number to the sum variable value using decision statements. If the values are equal, the user-provided number is shown as a perfect number.

Example:

``````new_input_val = int(input("Enter the value:"))
result = 0
for m in range(1, new_input_val ):
if new_input_val  % m == 0:
result = result + m
if result == new_input_val :
print(f"Value {new_input_val } is perfect number")
else:
print(f"Value {new_input_val } is not a perfect number")``````

Here is the implementation of the following given code

This is how we can check whether the number is perfect or not in Python.

## Python prints all perfect number

• Here we will discuss how to print all perfect numbers in Python.
• In this example, we have to print all the perfect numbers between the low number and the highest number but in this case, the highest number is infinite so we will declare a number that is 2000.
• To perform this particular, we will need to use the while loop concept and in this example, we will use the input user and enter the number. Next, we willg apply the for loop to generate numbers from 1 to n where n is not included as we need the sum of the proper divisors of the number.

Example:

Let’s take an example and check how to print all perfect numbers in Python.

Source Code:

``````new_start_val = int(input("Enter the beginning value of the range"))
new_end_val = int(input("Enter the last value of the range"))

for m in range(new_start_val, new_end_val+1):
result = 0

for z in range(1, m):
if m%z == 0:
result += z

if m == result:
print(m)``````

You can refer to the below Screenshot.

As you can see in the Screenshot we have understood how to print all perfect numbers in Python.

## Perfect number in Python using recursion

• In this example, we will discuss how to get the perfect number in Python by using the recursion method.
• A recursive function calls itself repeatedly. It functions similarly to the loops we previously discussed, although there are situations when recursion is preferable over loops.
• A base case and a recursive step are the two parts of every recursive function. The base case often contains the smallest input and a solution that is simple to verify. This method also prevents the function from calling itself indefinitely.

Example:

``````def perfect_number(n):

result = 0

for z in range(1,n):
if n%z==0:
result += z

return result == n

new_output = int(input("Enter the value:"))
if perfect_number(new_output):
print('It is a Perfect number',new_output)
else:
print('It is not a perfect number',new_output)``````

First, we will take input using an integer and store them in a variable. We will now define a variable named “result,” where we will store the total of the specified number’s divisors.

The following task is to create a for loop in which we will divide our number by the initialized value of the variable and then increase the value of I and determine which numbers result in a remainder of zero.

So, it will check the condition either it is a perfect number or not.

Here is the Output of the following given code.

You may also like to read the following Python tutorials.

In this tutorial, we have discussed how to find the perfect number in Python. And also we have covered the following given topics.

• How to find perfect number in Python
• How to find perfect number in Python using for loop
• How to find perfect number in Python using while loop
• How to find perfect number in Python using list
• How to find perfect square number in python
• How to find perfect number function in Python
• How to find python program for perfect numbers or not
• How to find python print all perfect numbers
• How to find perfect number in Python using recursion