谢谢大家
我已经编出来了
只运行3秒
大家看一下 发表一下意见 呵呵
#include <math.h>
#include <stdio.h>
#include <time.h>
/*打印3 - 1000000000的回文素数*/
/*打印出运行时间*/
/*打印出回文素数的个数*/
int Prime(int x);
int main()
{
int count = 4;
int a, b, c, d, e, f, g, h;
g = time(NULL);
printf("3\n5\n7\n11\n");
for (a = 1; a <= 9; a += 2)/*实现三位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
f = a * 101 + b * 10;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
for (a = 1; a <= 9; a += 2)/*实现五位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
f = a * 10001 + b * 1010 + c * 100;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
}
for (a = 1; a <= 9; a += 2)/*实现七位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
for (d = 0; d <= 9; d++)
{
f = a * 1000001 + b * 100010 + c * 10100 + d * 1000;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
}
}
for (a = 1; a <= 9; a += 2)/*实现九位数的回文素数*/
{
for (b = 0; b <= 9; b++)
{
for (c = 0; c <= 9; c++)
{
for (d = 0; d <= 9; d++)
{
for (e = 0; e <= 9; e++)
{
f = a * 100000001 + b * 10000010 + c * 1000100 + d * 101000 +e * 10000;
if (Prime(f))
{
count++;
printf("%d\n", f);
}
}
}
}
}
}
h = time(NULL);
printf("time = %d seconds\n",(h - g));
printf("count = %d\n",count);
return 0;
}
//函数功能:判断素数
//函数参数:整数
//函数返回值: 0 或 1
int Prime(int x)
{
int i;
double k;
k=sqrt(x);
for (i = 3;i <= k; i++)
{
if (x % i==0)
break;
}
if (i > k)
return 1;
else
return 0;
}