Check perfect number in python

Last update on August 19 2022 21:51:40 (UTC/GMT +8 hours)

Python Functions: Exercise-11 with Solution

Write a Python function to check whether a number is perfect or not.

According to Wikipedia : In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).
Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128.

Sample Solution:-

Python Code:

def perfect_number(n):
    sum = 0
    for x in range(1, n):
        if n % x == 0:
            sum += x
    return sum == n
print(perfect_number(6))
 

Sample Output:

True

Pictorial presentation:

Check perfect number in python

Flowchart:

Check perfect number in python

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to print the even numbers from a given list.
Next: Write a Python function that checks whether a passed string is palindrome or not.

Python: Tips of the Day

Optional Arguments:

We can pass in optional arguments by providing a default value to an argument:

def my_new_function(my_value='hello'):
  print(my_value)#Calling
my_new_function() => prints hello
my_new_function('test') => prints test

Playing with numbers is something that we are doing since our childhood. The basic arithmetic operations performed on numbers generate valuable results. In the voyage of learning, we must give more emphasis to the logic develop in mathematics as they become the foundation of programming.

In this article, let us discuss what is a perfect number, what are the steps to determine whether a number is perfect or not, and finally, we shall see how its program can be written in Python.

So, let's get started…

First, let us understand what is a perfect number?

A perfect number is a number in which the sum of the divisors of a number is equal to the number.

Make sure that we have to exclude the number when we are calculating the sum of divisors.

Now, we will see what are the steps involved in determining the perfect number.

  1. First of all, we will ask the user to input an integer which will be stored in a variable.
  2. Now, we will declare a variable called 'sum', where we will store the sum of the divisors of the given number.
  3. The next task is, to use a for loop where we will divide our number with the number initialized for the variable i, then we'll increment the value of I and check what are the numbers give us the remainder as zero. These numbers will be our divisors.
  4. Now we will take each of the divisors of that number and add it with the variable 'sum'.
  5. Finally, we will use the decision statement keyword 'if' to compare the number given by user with the value of the sum.
  6. If the values are equal, we will display the result as 'It's a perfect number', else we will display, 'It's not a perfect number'.

It's time to have a look at the Python program-

Example -

Output:

Enter the number: 6
The entered number is a perfect number
Enter the number: 25
The entered number is not a perfect number

Explanation -

In the program given above, we have-

  1. We have a variable 'num' and asked the user to provide its value.
  2. As discussed earlier, we have declared the variable sum_v as zero.
  3. Now, we have used the for loop whose range is from 1 to the number provided by the user.
  4. The next step is to check whether the number when divided with all the numbers provided in the range gives the remainder as zero.
  5. Those values will be added and then stored in the variable sum_v.
  6. Finally, we will make use of 'if' to compare the sum of divisors with the number and display the required result.
  7. In this program, the user has given the values 6 and 25 and the desired output is displayed.

Let us have a look at another program where we will implement the same with the help of a function.

Example - 2:

Output:

Explanation -

In this program, we have used steps similar to the ones we discussed in the previous example, the only difference is here we have defined a function and called it by providing a value.

Because the return statement is present at the end of a function definition, it gives the value as True and False.

So, in this tutorial, we learned about perfect numbers, the process that we must follow to determine whether the given number is perfect or not, and at the end discussed its implementation in Python.


How do you check if a number is a perfect number in Python?

Example -.
num=int(input("Enter the number: ")).
sum_v=0..
for i in range(1,num):.
if (num%i==0):.
sum_v=sum_v+i..
if(sum_v==num):.
print("The entered number is a perfect number").

How do you check if a number is a perfect?

A number is a perfect number if is equal to sum of its proper divisors, that is, sum of its positive divisors excluding the number itself. Write a function to check if a given number is perfect or not. Examples: Input: n = 15 Output: false Divisors of 15 are 1, 3 and 5.

How do you find the perfect number in a while loop in Python?

Find Perfect Number using While loop in Python.
Think what happen when num % i == 0 is False.. – balderman. Aug 2, 2021 at 7:10..
You have an infinite loop if num % i is not 0. Move i = i + 1 out of the if . – Guy. ... .
You have no else on your while , so if num % i != 0 you enter an infinite loop. Just put some else: i = i + 1..

What is the perfect number?

perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128. The discovery of such numbers is lost in prehistory.