找出回文素数


The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 1000,000,000); both a and b are considered to be within the range .
Input
Line 1: Two integers, a and b
Output
The list of palindromic primes in numerical order, one per line.
Sample Input
5 500
Sample Output
5
7
11
101
131
151
181
191
313
353
373
383


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int i;
int Is_Prime( int a)
{
     double t = a;
     for(i=2;i<=sqrt(t);i++)
    {
         if(a % i == 0)
             return 0;
    }
         return 1;
}
int diglen( int c)
{
     if(c/10 == 0)
         return 1;
     int count=0;
     while(c)
    {
        c=c/10;
        count++;
    }
     return count;
}

void IntoStr( int a, char* str)  // 将数字转换为字符串
{
     int h= diglen(a)-1,i=0;
     while(h+1)
    {
     int k,c,d=1;
     for(k=0;k<h;k++)
    {
        d=d*10;
    }

    c=a/d; // 取本次数字的最高位
    str[i++] = c+48;
    a = a%d;  // 取余数
    h--;
    }
    str[i]='\0';  // 最后末尾加字符串结束符
}

int Is_Pal( int b)
{
     int len = diglen(b);
     int ok = 1,j;
     char* str = ( char*)malloc( sizeof( char)*(len+1));
    IntoStr(b,str);  // 将数字转换为字符数组,也可以用itoa函数或 sprintf函数
     for(i=0,j=len-1;i < len/2;i++,j--)
    {
         if(str[i] != str[j])
            ok = 0;
        
    }
    free(str);
     if(ok == 1)
         return 1;
     else
         return 0;
}

int main()
{
     int a,b,j;
    scanf("%d%d",&a,&b);
     for(j=a;j<=b;j++)
    {
         if(Is_Prime(j) && Is_Pal(j))
            printf("%d\n",j);
    }
     return 0;
}

你可能感兴趣的:(找出回文素数)