-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path050.cpp
executable file
·73 lines (65 loc) · 2.14 KB
/
050.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
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <algorithm>
#include <cstdlib>
#define LIMIT 1000000
#define SIZE 78498
#define FIRST_PRIME 2
using namespace std;
void nextP(unsigned long long &p, unsigned long long array[]) {
for (unsigned long long i = p - 1; i < LIMIT - 2; i++) {
if (array[i] != 0) {
p = array[i];
return;
}
}
}
int compare(const void * a, const void * b) {
return (*(unsigned long long *) a - *(unsigned long long *) b);
}
void test(unsigned long long &n, unsigned long long primes[],
unsigned long long &size, unsigned long long &maxTerms,
unsigned long long &maxPrime, unsigned long long termsCount) {
if (n < LIMIT && termsCount > maxTerms &&
bsearch(&n, primes, size, sizeof(unsigned long long), compare)) {
maxTerms = termsCount;
maxPrime = n;
}
}
int main() {
unsigned long long tempPrimes[LIMIT - 2];
for (unsigned long long i = 0; i < LIMIT - 2; i++) tempPrimes[i] = i + 2;
for (unsigned long long p = FIRST_PRIME; p * p < LIMIT - 2;
nextP(p, tempPrimes)) {
for (unsigned long long n = p * p; n < LIMIT; n += p)
tempPrimes[n - 2] = 0;
}
unsigned long long count = 0;
for (unsigned long long i = 0; i < LIMIT - 2; i++)
if (tempPrimes[i] != 0) count++;
unsigned long long *primes = new unsigned long long[SIZE];
unsigned long long j = 0;
for (unsigned long long i = 0; i < LIMIT - 2; i++)
if (tempPrimes[i] != 0) primes[j++] = tempPrimes[i];
unsigned long long *sums = new unsigned long long[SIZE];
unsigned long long sum = 0;
for (unsigned long long i = 0; i < count; i++) {
sum += primes[i];
sums[i] = sum;
}
unsigned long long maxTerms = 0;
unsigned long long maxPrime = 0;
for (long long j = count - 1; j >= 0; j--) {
test(sums[j], primes, count, maxTerms, maxPrime, j + 1);
for (unsigned long long i = 0; i < min(count - j, (unsigned long long) j);
i++) {
unsigned long long termsCount = j - i;
unsigned long long currentSum = sums[j] - sums[i];
test(currentSum, primes, count, maxTerms, maxPrime, termsCount);
}
}
delete(primes);
delete(sums);
cout << maxPrime << endl;
cout << maxTerms << endl;
return 0;
}