-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircularp.c
107 lines (80 loc) · 1.88 KB
/
circularp.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
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<stdio.h>
#include<math.h>
/*A circular prime is a prime number with the property that
the number generated at each intermediate step when cyclically
permuting its (base 10) digits will be prime.
For example, 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.*/
int isprime(long long int num)
{
static long long int prime[100000]={2,3,5};
static long long int pctr=3;
int factor;
long int till=sqrt(num);
if(! (num%6==1 || num%6==5) )
return 0;
for(factor=0;prime[factor]<=till;factor++)
{
if((num%prime[factor])==0)
return 0;
}
prime[pctr++]=num;
return 1;
}
long long int circularnum(long long int num)
{
long long int safe,cnum , power = 1;
int digit;
safe=num;
while(num!=0)
{
num=num/10;
power=power*10;
}
num=safe;
power=power/10;
digit=num/power;
num=num%power;
cnum=num*10+digit;
return cnum;
}
int nod( long long int num )
{
int nod = 0 ;
while( num)
{
num = num / 10;
nod++;
}
return nod;
}
int main()
{
long long int flag = 0 ,safe , num,ctr,cnum , ctr1;
long long int prime[100000]={2,3,5},pctr=3;
for(num=7;num<1000000;num+=2)
{
if(isprime(num)==1)
prime[pctr++]=num;
}
for(ctr1 = 1 ; ctr1 < pctr ; ctr1++ )
{
num = prime[ ctr1 ];
safe = num ;
do
{
cnum = circularnum(num);
for( ctr = 0 ; ctr < pctr ; ctr++)
{
if( cnum == prime[ctr])
{
break;
}
}
if( ( nod(safe) == nod(cnum))&& safe!=cnum && ctr!=pctr)
num = cnum;
else break;
} while(1);
if( safe == cnum )
printf("%lld " , cnum);
}
}