-
Notifications
You must be signed in to change notification settings - Fork 0
/
id0060.c
99 lines (79 loc) · 2.57 KB
/
id0060.c
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
98
99
// Licensed under the MIT License.
// Prime Pair Sets
#include <limits.h>
#include <math.h>
#include "../lib/euler.h"
#include "../lib/sieve.h"
#define MAX_SEARCH 10000
static bool math_is_prime_pair(Sieve primes, int a, int b)
{
long concat = math_concat(a, b);
PrimalityTest primalityTest = miller_rabin_primality_test;
if (sieve_test(primes, concat, primalityTest) != PRIMALITY_PRIME)
{
return false;
}
concat = math_concat(b, a);
if (sieve_test(primes, concat, primalityTest) != PRIMALITY_PRIME)
{
return false;
}
return true;
}
int main(void)
{
int min = INT_MAX;
struct Sieve primes;
struct List candidates;
clock_t start = clock();
euler_ok(sieve(&primes, MAX_SEARCH));
euler_ok(list(&candidates, sizeof(long long), 0));
long long* primesBegin = primes.primes.items;
long long* primesEnd = primesBegin + primes.primes.count;
for (long long* a = primesBegin; a < primesEnd; a++)
{
list_clear(&candidates);
for (long long* b = a; b < primesEnd; b++)
{
if (math_is_prime_pair(&primes, *a, *b))
{
euler_ok(list_add(&candidates, b));
}
}
long long* candidatesBegin = candidates.items;
long long* candidatesEnd = candidatesBegin + candidates.count;
for (long long* b = candidatesBegin; b < candidatesEnd; b++)
{
for (long long* c = b + 1; c < candidatesEnd; c++)
{
if (!math_is_prime_pair(&primes, *b, *c))
{
continue;
}
for (long long* d = c + 1; d < candidatesEnd; d++)
{
if (!math_is_prime_pair(&primes, *b, *d) ||
!math_is_prime_pair(&primes, *c, *d))
{
continue;
}
for (long long* e = d + 1; e < candidatesEnd; e++)
{
int sum = *a + *b + *c + *d + *e;
if (sum >= min ||
!math_is_prime_pair(&primes, *b, *e) ||
!math_is_prime_pair(&primes, *c, *e) ||
!math_is_prime_pair(&primes, *d, *e))
{
continue;
}
min = sum;
}
}
}
}
}
finalize_sieve(&primes);
finalize_list(&candidates);
return euler_submit(60, min, start);
}