求不超过1000的回文素数。
代码如下:
- #include <stdio.h>
- #include <math.h>
- int isNumber(int number);
- int main(int argc, char *argv[])
- {
- int i, j, t, k, s;
- printf("following are palindrome primes not greater than 1000:\n");
- for(i=0; i<10; i++)
- for(j=0; j<10; j++)
- for(k=0; k<10; k++){
- s = i*100 + j*10 + k;
- t = k*100 + j*10 + i;
- if(i==0 && j==0)
- t /= 100;
- else if(i==0)
- t /= 10;
- else
- ;
- if(s>10 && s==t && isNumber(s))
- printf("%5d", s);
- }
- printf("\n");
- return 0;
- }
- int isNumber(int number)
- {
- int i, j;
- if(number%2 == 0)
- return 0;
- j = sqrt((double)number) + 1;
- for(i=3; i<=j; i+=2){
- if(number % i == 0)
- return 0;
- }
- return 1;
- }
执行结果如下:
following are palindrome primes not greater than 1000:
11 101 131 151 181 191 313 353 373 383 727 757 787 797 919 929
阅读(2943) | 评论(0) | 转发(0) |