-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path060_prime_pair_sets.cpp
97 lines (81 loc) · 3.58 KB
/
060_prime_pair_sets.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any
* order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime.
* The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.
*
* Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.
*/
#include <vector>
#include <iostream>
#include "MyLib_cpp.h"
using namespace std;
vector<num> witnesses = {2, 7, 61};
bool checkPair(unsigned int p1, unsigned int p2) {
return millerRabin(stol(to_string(p1) + to_string(p2)), witnesses)
&& millerRabin(stol(to_string(p2) + to_string(p1)), witnesses);
}
int main(int argc, char **argv) {
int jo = 0;
// vector<num> w = {2, 3, 5};
vector<num> w = {2, 31, 73};
if (millerRabin(31, w))
cout << "prime" << endl;
else
cout << "not prime" << endl;
#pragma omp parallel for schedule(dynamic, 500) reduction(+:jo)
for (unsigned int i = 2; i < 9080191; ++i) {
if (millerRabin(i, w))
++jo;
}
cout << jo << endl;
return 42;
// init with estimates about the problem
unsigned int bestSum = 35000;
unsigned int minSum = 20000;
vector<unsigned int> primes = sieveOfEratosthenes(20000);
primes.erase(primes.begin());
cout << "Using " << primes.size() << " primes from " << primes[0] << " to " << primes[primes.size() - 1] << endl;
int lol = 42;
for (unsigned int p1 = 0; p1 < primes.size(); ++p1) {
if (primes[p1] * 5 >= bestSum)
break;
for (unsigned int p2 = p1 + 1; p2 < primes.size(); ++p2) {
if (primes[p1] + primes[p2] * 4 >= bestSum)
break;
if (!checkPair(primes[p1], primes[p2]))
continue;
for (unsigned int p3 = p2 + 1; p3 < primes.size(); ++p3) {
if (primes[p1] + primes[p2] + primes[p3] * 3 >= bestSum)
break;
if (!checkPair(primes[p1], primes[p3])
|| !checkPair(primes[p2], primes[p3]))
continue;
for (unsigned int p4 = p3 + 1; p4 < primes.size(); ++p4) {
if (primes[p1] + primes[p2] + primes[p3] + primes[p4] * 2 >= bestSum)
break;
if (!checkPair(primes[p1], primes[p4])
|| !checkPair(primes[p2], primes[p4])
|| !checkPair(primes[p3], primes[p4]))
continue;
for (unsigned int p5 = p4 + 1; p5 < primes.size(); ++p5) {
unsigned int primeSum = primes[p1] + primes[p2] + primes[p3] + primes[p4] + primes[p5];
if (primeSum >= bestSum || primeSum <= minSum)
break;
if (!checkPair(primes[p1], primes[p5])
|| !checkPair(primes[p2], primes[p5])
|| !checkPair(primes[p3], primes[p5])
|| !checkPair(primes[p4], primes[p5]))
continue;
bestSum = primeSum;
cout << "New best: " << bestSum << " | "
<< primes[p1] << ", "
<< primes[p2] << ", "
<< primes[p3] << ", "
<< primes[p4] << ", "
<< primes[p5] << endl;
}
}
}
}
}
}