求四位的可逆素数。可逆素数指:一个素数将其各位数字的顺序倒过来构成的反序数也是素数。
代码如下:
- #include <stdio.h>
- #include <math.h>
- int num(int number);
- int isNumber(int number);
- int main(int argc, char *argv[])
- {
- int i, count;
- printf("there are invertable primes with 4 digits:\n");
- for(count=0, i=1001; i<10000; i+=2){
- if(num(i))
- printf(count%9 ? "%3d:%d ": "%3d:%d\n", ++count, i);
- }
- printf("\n");
- return 0;
- }
- int num(int number)
- {
- int i, j;
- if(!isNumber(number))
- return 0;
- for(i=number, j=0; i>0; i/=10){
- j = j*10 + i%10;
- }
- if(number < j){
- if(!(isNumber(j)))
- return 0;
- else
- return 1;
- }
- 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;
- }
执行结果如下:
there are invertable primes with 4 digits:
1:1009 2:1021 3:1031 4:1033 5:1061 6:1069 7:1091 8:1097 9:1103
10:1109 11:1151 12:1153 13:1181 14:1193 15:1213 16:1217 17:1223 18:1229
19:1231 20:1237 21:1249 22:1259 23:1279 24:1283 25:1381 26:1399 27:1409
28:1429 29:1439 30:1453 31:1471 32:1487 33:1499 34:1523 35:1559 36:1583
37:1597 38:1619 39:1657 40:1669 41:1723 42:1733 43:1753 44:1789 45:1847
46:1867 47:1879 48:1913 49:1933 50:1949 51:1979 52:3019 53:3023 54:3049
55:3067 56:3083 57:3089 58:3109 59:3163 60:3169 61:3257 62:3299 63:3319
64:3343 65:3347 66:3359 67:3373 68:3389 69:3407 70:3463 71:3467 72:3469
73:3527 74:3583 75:3697 76:3719 77:3767 78:3889 79:3917 80:3929 81:7027
82:7057 83:7177 84:7187 85:7219 86:7229 87:7297 88:7349 89:7457 90:7459
91:7529 92:7577 93:7589 94:7649 95:7687 96:7699 97:7879 98:7949 99:9029
100:9349 101:9479 102:9679
阅读(3937) | 评论(0) | 转发(0) |