-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_49.c
52 lines (45 loc) · 913 Bytes
/
problem_49.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
#include <stdio.h>
#include <math.h>
#define DIGITS 4
static int is_prime( unsigned long p )
{
if( p < 2 ) {
return 0;
}
unsigned long i;
for( i = 2; i <= sqrt( p ); i++ ) {
if( p % i == 0 ) {
return 0;
}
}
return 1;
}
static int n_digit( int x, int n )
{
return (int) ( x / pow( 10, n ) ) % 10;
}
static int is_permutate( int a, int b, int c )
{
int a_mask, b_mask, c_mask, i;
a_mask = b_mask = c_mask = i = 0;
for( i = 0; i < DIGITS; i++ ) {
a_mask |= 1 << n_digit( a, i );
b_mask |= 1 << n_digit( b, i );
c_mask |= 1 << n_digit( c, i );
}
return ( a_mask == b_mask ) && ( b_mask == c_mask );
}
int main( void )
{
int a, b, c;
for( a = 1111; a < 9999 - 6660; a++ ) {
b = a + 3330;
c = b + 3330;
if( is_permutate( a, b, c ) &&
is_prime( a ) &&
is_prime( b ) &&
is_prime( c ) ) {
printf( "%u %u %u\n", a, b, c );
}
}
}