Check whether a given number is perfect number or not

Check whether a given number is perfect number or not

A program to check whether a given number is a perfect number or not is discussed here. A perfect number is a number which is equal to the sum of its proper positive divisors.


Check whether a given number is perfect number or not

For example, 6 is a perfect number.nThe divisors of 6 are 1, 2 and 3.n1 + 2 + 3 = 6.n


This problem can be solved in two different ways.


Method 1: Starting from 1 to (num – 1), find all the divisors of the number. If the sum of divisors of the number is equal to the number, return true.

Method 2: Starting from 1 to (num^1/2),find all the divisors of the number. If the sum of divisors of the number is equal to the number, return true.


The second method is much more efficient than the first method.


Algorithm to check whether a given number is a perfect number or not

  • Input the number.
  • Find all divisors of the number except the number itself.
  • If the sum of all divisors of the number is equal to the number, then return true. Else, return false.


Check whether a given number is perfect number or not

Perfect number or not using method 1

@@coding::1@@


Time complexity: O(n)


Check whether a given number is perfect number or not

Perfect number or not using method 2 (more efficient)


@@coding::2@@


Time complexity: O(n^1/2)


Recommended Programs












Check whether a given number is perfect number or not

c