-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_35.c
59 lines (53 loc) · 983 Bytes
/
problem_35.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
#include <stdio.h>
#include <math.h>
static int is_prime( unsigned long p )
{
if( p < 2 ) {
return 0;
}
if( p == 2 ) return 1;
if( p % 2 == 0 ) return 0;
unsigned long i;
for( i = 3; i <= sqrt( p ); i+=2 ) {
if( p % i == 0 ) {
return 0;
}
}
return 1;
}
int rotate( int x )
{
int r = x;
int n = 1;
while( r / 10 ) { r /= 10; n *= 10; }
return 10 * ( x - r * n ) + r;
}
int has_zero( int x )
{
while( x ) {
if( x % 10 == 0 ) return 1;
x /= 10;
}
return 0;
}
int main( )
{
int n = 1000000;
int num = 0;
while( n-- ) {
if( !is_prime( n ) ) continue;
if( has_zero( n ) ) continue;
int r = n;
int circular = 1;
while( (r = rotate(r)) != n ) {
if( !is_prime( r ) ) {
circular = 0;
break;
}
}
if( circular ) {
num++;
}
}
printf( "Answer: %d\n", num );
}