Given an even number N, the task is to check whether it is a Perfect number or not without finding its divisors.
In number theory, an Even Perfect Number is a positive integer which is even or that is equal to the sum of its positive divisors, exccluding the number itself.
An even perfect number can be represented as P * (P + 1) / 2 where P is Mersenne Prime.
A Mersenne Prime is a prime number of form 2q – 1 where q is also a prime number.
For example: if N = 6,
If we choose q to be 2 (prime number) then mersenne prime (P) is 22 – 1 = 3.
Therefore, the Even perfect number formed by the formula is 3 * (3 + 1) / 2 = 6.
Input: N = 6
The integer 6 can be written as 6 = 1 + 2 + 3. Hence, its perfect number.
Input: N =156
The integer 156 cannot be written as a sum of its divisors. Hence, its not a perfect number.
- Find the square root of the given number to get a number close to 2q – 1.
- Find q-1 from the square root of the number and then check whether 2q-1 * (2q-1) gives the number entered. If not then it is not a perfect number, otherwise continue.
- Check whether q is prime or not. If it is not prime then 2q-1 cannot be prime and subsequently check whether 2q-1 is prime.
- If all the above conditions hold true then it is an even perfect number otherwise not.
Below is the implementation of the above approach:
Time Complexity: O(N1/4)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Count numbers upto N which are both perfect square and perfect cube
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Count ways to express even number ‘n’ as sum of even integers
- Average of even numbers till a given even number
- Check if a number has an odd count of odd divisors and even count of even divisors
- Sum of even numbers at even position
- Print even positioned nodes of even levels in level order of the given binary tree
- Rearrange array such that all even-indexed elements in the Array is even
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Minimum divisor of a number to make the number perfect cube
- Previous perfect square and cube number smaller than number N
- Number of perfect squares between two given numbers
- Count all perfect divisors of a number
- Perfect Number
- Largest number that is not a perfect square
- Program to check Plus Perfect Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.