-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid0037.c
56 lines (42 loc) · 1.05 KB
/
id0037.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
// Licensed under the MIT License.
// Truncatable Primes
#include <math.h>
#include "../lib/euler.h"
#include "../lib/sieve_iterator.h"
int main(void)
{
long sum = 0;
int count = 0;
struct Sieve primes;
struct SieveIterator it;
clock_t start = clock();
euler_ok(sieve(&primes, 0));
sieve_begin(&it, &primes);
for (sieve_skip(&it, 4); count < 11; sieve_next(&it))
{
long right = it.current;
while (right && sieve_test(&primes, right, NULL) == PRIMALITY_PRIME)
{
right /= 10;
}
if (right)
{
continue;
}
long left = it.current;
long orderOfMagnitude = pow(10, (int)log10(left));
while (left && sieve_test(&primes, left, NULL) == PRIMALITY_PRIME)
{
left %= orderOfMagnitude;
orderOfMagnitude /= 10;
}
if (left)
{
continue;
}
sum += it.current;
count++;
}
finalize_sieve(&primes);
return euler_submit(37, sum, start);
}