-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10490.cpp
63 lines (55 loc) · 1.37 KB
/
P10490.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Perfect numbers from https://oeis.org/A000396
long perfect[8] = {6, 28, 496, 8128, 33550336, 8589869056, 137438691328L, 2305843008139952128L};
#define PRIME_LEN 50
class PrimeHandler {
bitset<PRIME_LEN> primes;
public:
void init() {
primes.set();
// Sieve primes:
for(int i = 0; i*i < PRIME_LEN; ++i) {
if(!primes[i])
continue;
// Mark all uneven multiples as non-prime:
int basePrime = 1+2*(i+1);
for(int multiple = 3; true; multiple += 2) {
int notAPrime = basePrime*multiple;
int notAPrimeI = notAPrime/2-1;
if(notAPrimeI >= PRIME_LEN)
break;
primes.set(notAPrimeI, false);
}
}
}
bool isPrime(long n) const {
if(n == 2)
return true;
if(n < 2 || (n%2==0))
return false;
return primes[n/2-1];
}
};
int main() {
PrimeHandler ph;
ph.init();
set<long> allPerfect;
FORI(8)
allPerfect.insert(perfect[i]);
int k;
while(cin >> k) {
if(k < 1)
return 0;
if(!ph.isPrime(k)) {
cout << "Given number is NOT prime! NO perfect number is available." << endl;
continue;
}
long part1 = 1 << (k-1);
long part2 = (1 << k)-1;
long n = part1*part2;
if(allPerfect.find(n) != allPerfect.end())
cout << "Perfect: " << n << "!" << endl;
else
cout << "Given number is prime. But, NO perfect number is available." << endl;
}
return 0;
}