-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path462.cpp
More file actions
68 lines (65 loc) · 1.64 KB
/
Copy path462.cpp
File metadata and controls
68 lines (65 loc) · 1.64 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
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
64
65
66
67
68
#include <iostream>
#include <cstring>
#define MAX 600
#define RANGE 3500000
//3448275
using namespace std;
int main() {
int* prime = new int[5761460];
int* isNotPrime = new int[RANGE];
memset(isNotPrime, 0, sizeof(int) * RANGE);
prime[0] = 0;
//isNotPrime[3] = 1;
int target;
for (int i = 3; i <= MAX; i+=2) {
if (((isNotPrime[i % RANGE] >> (i / RANGE)) & 1) == 0) {
prime[++prime[0]] = i;
cout << i << endl;
}
for (int j = 1; j <= prime[0] && i*prime[j] <= MAX; j++) {
isNotPrime[(i*prime[j]) % RANGE] += 1 << (i*prime[j] / RANGE);
//cout << " " << i*prime[j] << endl;
if (i % prime[j] == 0) {
break;
}
}
}
while (cin >> target) {
bool success = false;
if (target <= 4) {
cout << target << " is not the sum of two primes!" << endl;
continue;
}
if (target % 2 == 1) {
if (((isNotPrime[(target - 2) % RANGE] >> ((target - 2) / RANGE)) & 1) == 0) {
cout << target << " is the sum of " << 2 << " and " << target - 2 << "." << endl;
}
else {
cout << target << " is not the sum of two primes!" << endl;
}
continue;
}
else {
int start = target / 2;
if (start % 2 == 0) {
start++;
}
else {
start += 2;
}
for (int i = start; i < target; i += 2) {
if (((isNotPrime[i % RANGE] >> (i / RANGE)) & 1) == 0 && ((isNotPrime[(target - i) % RANGE] >> ((target - i) / RANGE)) & 1) == 0 && target - i > 2) {
cout << target << " is the sum of " << target - i << " and " << i << "." << endl;
success = true;
break;
}
}
if (!success) {
cout << target << " is not the sum of two primes!" << endl;
}
}
}
delete[] prime;
delete[] isNotPrime;
return 0;
}