VOOZH about

URL: https://www.geeksforgeeks.org/dsa/perfect-number/

⇱ Perfect Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Perfect Number

Last Updated : 23 Jul, 2025

A number is a perfect number if it is equal to the sum of its proper divisors, that is, the sum of its positive divisors excluding the number itself. Find whether a given positive integer n is perfect or not.
Examples:

Input: n = 15
Output: false
Explanation: Divisors of 15 are 1, 3 and 5. Sum of divisors is 9 which is not equal to 15.

Input: n = 6
Output: true
Explanation: Divisors of 6 are 1, 2 and 3. Sum of divisors is 6.

[Naive Approach] Divisor Sum Method - O(n) Time and O(1) Space

A simple Solution is to go through every number from 1 to n-1 and check if it is a divisor. Maintain sum of all divisors. If sum becomes equal to n, then return true, else return false.


Output
false

[Expected Approach] Optimized Divisor Search Method - O(sqrt n) Time and O(1) Space

An efficient Solution is to go through numbers till square root of n. If a number 'i' divides n, then add both 'i' and 'n/i' .


Output
false

Interesting facts About Perfect Number
1) Every even perfect number is of the form 2pāˆ’1ā‹…(2pāˆ’1) , where 2pāˆ’1 is a prime number.
2) It is unknown whether there are any odd perfect numbers.

Comment
Article Tags:
Article Tags: