-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem37.c
67 lines (57 loc) · 961 Bytes
/
problem37.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
/*
* Copyright (c) 2009, eagletmt
* Released under the MIT License <http://opensource.org/licenses/mit-license.php>
*/
#include <stdio.h>
#include <math.h>
static int is_prime(unsigned n);
int main(void)
{
unsigned i;
unsigned count = 0, sum = 0;
for (i = 10; count < 11; i++) {
/* truncate from right to left */
unsigned k, d;
k = i;
while (k != 0) {
if (!is_prime(k)) {
goto NEXT;
}
k /= 10;
}
/* truncate from left to right */
k = i;
d = log10(k);
d = pow(10, d);
while (d != 1) {
if (!is_prime(k % d)) {
goto NEXT;
}
d /= 10;
}
sum += i;
count++;
NEXT:
;
}
printf("%u\n", sum);
return 0;
}
int is_prime(unsigned n)
{
if (n <= 1) {
return 0;
}
else if (n == 2) {
return 1;
}
else {
unsigned i;
for (i = 2; i*i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
}