-
Notifications
You must be signed in to change notification settings - Fork 0
/
id0049.c
94 lines (78 loc) · 2.26 KB
/
id0049.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
// Licensed under the MIT License.
// Prime Permutations
#include "../lib/euler.h"
#include "../lib/string_builder.h"
#include "../lib/permutation_iterator.h"
#include "../lib/sieve.h"
static void math_prime_permutation(
Sieve primes,
StringBuilder aDigits,
StringBuilder bDigits,
StringBuilder cDigits,
int min,
int max,
long long* result)
{
for (int a = min; a < max; a++)
{
int b = a + 3330;
int c = b + 3330;
if (sieve_test(primes, a, NULL) != PRIMALITY_PRIME ||
sieve_test(primes, b, NULL) != PRIMALITY_PRIME ||
sieve_test(primes, c, NULL) != PRIMALITY_PRIME)
{
continue;
}
string_builder_clear(aDigits);
euler_ok(string_builder_append_format(aDigits, "%d", a));
string_builder_clear(bDigits);
euler_ok(string_builder_append_format(bDigits, "%d", b));
if (!permutation_test(
aDigits->buffer,
aDigits->length,
bDigits->buffer,
bDigits->length,
1,
char_equality_comparer))
{
continue;
}
string_builder_clear(cDigits);
euler_ok(string_builder_append_format(cDigits, "%d", c));
if (!permutation_test(
bDigits->buffer,
bDigits->length,
cDigits->buffer,
cDigits->length,
1,
char_equality_comparer))
{
continue;
}
*result = a * 100000000ll + b * 10000 + c;
}
}
int main(void)
{
long long result = -1;
struct Sieve primes;
struct StringBuilder a;
struct StringBuilder b;
struct StringBuilder c;
clock_t start = clock();
euler_ok(sieve(&primes, 10000));
euler_ok(string_builder(&a, 0));
euler_ok(string_builder(&b, 0));
euler_ok(string_builder(&c, 0));
math_prime_permutation(&primes, &a, &b, &c, 2, 1487, &result);
if (result < 0)
{
long long* begin = primes.primes.items;
int last = begin[primes.primes.count - 1];
math_prime_permutation(&primes, &a, &b, &c, 1488, last, &result);
}
finalize_string_builder(&a);
finalize_string_builder(&b);
finalize_string_builder(&c);
return euler_submit(49, result, start);
}