Perfect number program in Python

Perfect number in Python

According to number theory, a limb of pure mathematics that deals with integers, a Perfect Number can be defined as a positive integer whose value is equivalent to the sum of its proper positive divisors, excluding the number itself (alternatively known as aliquot sum).

An example of a perfect number is the number 6, the first perfect number in the series. The numbers 1, 2, and 3 are its proper divisors, and the sum of all three proper divisors gives us the original number, which makes it a perfect number.

In this tutorial, we will discuss different ways to check whether a number is a perfect number in python.

Use the Simple iteration method to check whether a given number is a perfect number.

In the Simple iteration method, we iterate through all the numbers from 1 to the desired number x to check if that specific number is a proper divisor. The code returns a true value if the sum of all the proper divisors is found to be equal to the number x, otherwise, it returns false.

The following code implements the Simple iteration method to check whether a given number is a perfect number.




  • Created a function, which takes in the value x, which is the original number that needs to be evaluated.
  • We create a loop or an iteration for generating the numbers from 1 to x, and then the if statement to check if the number divided by the variable i gives no remainder and is a proper divisor.
  • All the proper divisors that are found out by the iteration are added together, and if the sum of proper divisors is equivalent to the original number x, then it is a perfect number. The result is then printed.

Use the square root method to check whether a given number is a perfect number.

Another method, which is arguably the more efficient method out of the two, is the square root method.

In this method, we iterate through all the numbers up until the square root of n is encountered. The basic concept is to add both i and x/i to the sum s, if the number i divides the original number x which is to be checked for a perfect number.

The following code implements the square root method to check whether a given number is a perfect number.



Time complexity of an algorithm or a code can be defined as the total time that is essential for the program to run till its execution gets terminated. The first method has the time complexity of O(n), whereas the second method, which is the square root method, has the time complexity O(n^1/2).

Therefore, due to this difference in time complexities between the two methods, the second (square root) method is considered to be much more efficient than the simple iteration method.

That’s all about Perfect number in Python.

Related Posts

  • Calculator program in Python
    19 June

    Calculator program in Python

    Table of ContentsUsing the while loop along with the if...else conditional statement.Define functions for Addition, Subtraction, Multiplication and DivisionTake user input using input functionComplete calculator program in Python A simple Calculator can be utilized to carry out the four basic arithmetic operations namely addition, division, multiplication, and subtraction depending on the input of the user. […]

  • 07 June

    Number guessing game in Python

    Table of ContentsNumber guessing game RulesNumber guessing game implementation in PythonJava implementation A number guessing game is a common mini-project for basic programmers who have a grasp on random number generation and conditional statements with iteration. The number guessing game is based on a concept where player has to guess a number between given range. […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.