-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfect Number
More file actions
34 lines (32 loc) · 1.29 KB
/
Perfect Number
File metadata and controls
34 lines (32 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
*Euclid proved that 2p−1(2p−1) is an even perfect number whenever 2p−1 is prime, where ppp is prime.
*
*For example, the first four perfect numbers are generated by the formula 2p−1(2p−1), with ppp a prime number, as follows:
*
*for p = 2: 21(22 − 1) = 6
*for p = 3: 22(23 − 1) = 28
*for p = 5: 24(25 − 1) = 496
*for p = 7: 26(27 − 1) = 8128.
*
*Prime numbers of the form 2p−1 are known as Mersenne primes. For 2p−1 to be prime, it is necessary that ppp itself be prime.
*However, not all numbers of the form 2p−1 with a prime ppp are prime; for example, 211−1=2047=23×89 is not a prime number.
*
*You can see that for small value of ppp, its related perfect number goes very high. So, we need to evaluate perfect numbers
*for some primes (2,3,5,7,13,17,19,31) only, as for bigger prime its perfect number will not fit in 64 bits.
*/
class Solution {
private:
int primeN(int prime){
return (1 << (prime - 1)) * ((1 << prime) - 1);
}
public:
bool checkPerfectNumber(int num) {
std::vector<int> primes={2, 3, 5, 7, 13, 17, 19, 31};
for(unsigned int cpt=0; cpt<primes.size(); cpt++){
if(primeN(primes[cpt])==num){
return true;
}
}
return false;
}
};