The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
#define N 1000000
bool prime(char a[])
{
int x = atoi(a);
for (int i = 2; i*i <=x;i++)
{
if (x % i == 0)
{
return false;
}
}
return true;
}
void transmit(char a[])
{
char t = a[0];
for (int i = 1; a[i] != '\0'; i++)
{
a[i-1] = a[i];
}
a[i-1] = t;
}
int main()
{
clock_t ts,te;
ts=clock();
int answer = 0;
char a[6];
bool t = true;
for (int i = 2,k = 0; i < N;i++)
{
sprintf(a,"%d",i);
t = true;
for (int j = 0; j < strlen(a);j++)
{
if (prime(a) != true )
{
t = false;
break;
}
transmit(a);
}
if (t == true)
{
printf("%10d\n",i);
k++;
answer++;
}
}
printf("\nanswer %d",answer);
te=clock();
printf("\ntime difference: %ds\n",(te-ts)/CLOCKS_PER_SEC);
getchar();
return 0;
}