其他分享
首页 > 其他分享> > 4N - 素数回文

4N - 素数回文

作者:互联网

xiaoou33对既是素数又是回文的数特别感兴趣。比如说151既是素数又是个回文。现在xiaoou333想要你帮助他找出某个范围内的素数回文数,请你写个程序找出 a 跟b 之间满足条件的数。(5 <= a < b <= 100,000,000); 

Input

这里有许多组数据,每组包括两组数据a跟b。

Output

对每一组数据,按从小到大输出a,b之间所有满足条件的素数回文数(包括a跟b)每组数据之后空一行。

Sample Input

5 500

Sample Output

5
7
11
101
131
151
181
191
313
353
373
383

// 判断输入的数是否为素数,若是,则将它转为字符串判断它是否为回文
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 char num[100000001];
 5 int palindrome(char str[])
 6 {
 7     int i, len, flag=1;
 8     len=strlen(str);
 9     for(i=0;i<len/2;i++)
10         if(str[i]!=str[len-1-i])
11         { flag=0; break; }
12     return flag;
13 }
14 
15 int prime(int n)
16 {
17     int i, flag=1;
18     for(i=2; i<n; i++)
19         if(n%i==0)
20         { flag=0; break; }
21     return flag;
22 }
23 
24 int main()
25 {
26     int a, b, i;
27     while(scanf("%d %d", &a, &b)!=EOF)
28     {
29         for(i=a;i<=b;i++)
30             if(prime(i))
31             {
32                 itoa(i,num,10);
33                 if(palindrome(num))
34                     puts(num);
35             }
36         printf("\n");
37     }
38     return 0;
39 }
Time Limit Exceeded
// 缩短了判断素数时遍历的范围
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<math.h>
 4 #include<string.h>
 5 char num[100000001];
 6 int palindrome(char str[])
 7 {
 8     int i, len, flag=1;
 9     len=strlen(str);
10     for(i=0;i<len/2;i++)
11         if(str[i]!=str[len-1-i])
12         { flag=0; break; }
13     return flag;
14 }
15 
16 int prime(int n)
17 {
18     int i, flag=1, k;
19     k=(int)ceil(sqrt(n*1.0));
20     for(i=2; i<=k; i++)
21         if(n%i==0)
22         { flag=0; break; }
23     return flag;
24 }
25 
26 int main()
27 {
28     int a, b, i;
29     while(scanf("%d %d", &a, &b)!=EOF)
30     {
31         for(i=a;i<=b;i++)
32             if(prime(i))
33             {
34                 itoa(i,num,10);
35                 if(palindrome(num))
36                     puts(num);
37             }
38         printf("\n");
39     }
40     return 0;
41 }
Time Limit Exceeded*2
// 判整数型回文方法见代码
// 判素数中的终止循环条件中无需调用sqrt
 1 #include<stdio.h>
 2 int palindrome(int x)
 3 {
 4     int i=x, j=0, flag=0;
 5     while(i)
 6     { j=j*10+i%10; i/=10; }
 7     if(j==x) flag=1;
 8     return flag;
 9 }
10 
11 int prime(int n)
12 {
13     int i, flag=1, k;
14     if(n!=2&&n%2==0) flag=0;
15     else
16     {
17         for(i=2; i*i<=n; i++)
18             if(n%i==0)
19             { flag=0; break; }
20     }
21     return flag;
22 }
23 
24 int main()
25 {
26     int a, b, i;
27     while(scanf("%d %d", &a, &b)!=EOF)
28     {
29         for(i=a;i<=b;i++)
30             if(prime(i))
31                 if(palindrome(i))
32                     printf("%d\n", i);
33         printf("\n");
34     }
35     return 0;
36 }
Time Limit Exceeded*3
// 偶数除2外都不是素数
// 给定范围内最大的素数回文为9989899.
// 除了11外,任意偶数长度的回文都是11的倍数
 1 #include<stdio.h>
 2 int palindrome(int x)
 3 {
 4     int i=x, j=0, flag=0;
 5     while(i)
 6     { j=j*10+i%10; i/=10; }
 7     if(j==x) flag=1;
 8     return flag;
 9 }
10 
11 int prime(int n)
12 {
13     int i, flag=1;
14     for(i=3; i*i<=n; i++)
15         if(n%i==0)
16         { flag=0; break; }
17     return flag;
18 }
19 
20 int main()
21 {
22     int a, b, i;
23     while(scanf("%d %d", &a, &b)!=EOF)
24     {
25         for(i=a%2?a:a+1;i<=b&&i<=9989899;i+=2)
26         {
27             if(i>11&&i<100 || i>=1000&&i<10000 || i>=100000&&i<1000000)
28                 continue;
29             if(prime(i)&&palindrome(i))
30                 printf("%d\n", i);
31         }
32         printf("\n");
33     }
34     return 0;
35 }
Time Limit Exceeded*4
// 打表。。。一次遍历输出
 1 #include<stdio.h>
 2 #include<process.h>
 3 
 4 int palindrome(int x)
 5 {
 6     int i=x, j=0, flag=0;
 7     while(i)
 8     { j=j*10+i%10; i/=10; }
 9     if(j==x) flag=1;
10     return flag;
11 }
12 
13 int prime(int n)
14 {
15     int i, flag=1;
16     for(i=2; i*i<=n; i++)
17         if(n%i==0)
18         { flag=0; break; }
19     return flag;
20 }
21 
22 int main()
23 {
24     FILE *fp;
25     if((fp=fopen("Ntest.txt","wt"))==NULL)
26     {
27         printf("Error!\n");
28         exit(0);
29     }
30     int j=0;
31     for(int i=5;i<=100000000;i++)
32         if(prime(i)&&palindrome(i))
33         {
34             j++;
35             if(j%5) fprintf(fp, "%d, ", i);
36             else    fprintf(fp, "%d, \n", i);    
37         }
38     fprintf(fp, "\n\n%d", j);
39     printf("Done!\n");
40     fclose(fp);
41     return 0;
42 }
打表代码
  1 #include<stdio.h>
  2 #define N 779
  3 
  4 int table[N]=
  5 {
  6 5, 7, 11, 101, 131, 
  7 151, 181, 191, 313, 353, 
  8 373, 383, 727, 757, 787, 
  9 797, 919, 929, 10301, 10501, 
 10 10601, 11311, 11411, 12421, 12721, 
 11 12821, 13331, 13831, 13931, 14341, 
 12 14741, 15451, 15551, 16061, 16361, 
 13 16561, 16661, 17471, 17971, 18181, 
 14 18481, 19391, 19891, 19991, 30103, 
 15 30203, 30403, 30703, 30803, 31013, 
 16 31513, 32323, 32423, 33533, 34543, 
 17 34843, 35053, 35153, 35353, 35753, 
 18 36263, 36563, 37273, 37573, 38083, 
 19 38183, 38783, 39293, 70207, 70507, 
 20 70607, 71317, 71917, 72227, 72727, 
 21 73037, 73237, 73637, 74047, 74747, 
 22 75557, 76367, 76667, 77377, 77477, 
 23 77977, 78487, 78787, 78887, 79397, 
 24 79697, 79997, 90709, 91019, 93139, 
 25 93239, 93739, 94049, 94349, 94649, 
 26 94849, 94949, 95959, 96269, 96469, 
 27 96769, 97379, 97579, 97879, 98389, 
 28 98689, 1003001, 1008001, 1022201, 1028201, 
 29 1035301, 1043401, 1055501, 1062601, 1065601, 
 30 1074701, 1082801, 1085801, 1092901, 1093901, 
 31 1114111, 1117111, 1120211, 1123211, 1126211, 
 32 1129211, 1134311, 1145411, 1150511, 1153511, 
 33 1160611, 1163611, 1175711, 1177711, 1178711, 
 34 1180811, 1183811, 1186811, 1190911, 1193911, 
 35 1196911, 1201021, 1208021, 1212121, 1215121, 
 36 1218121, 1221221, 1235321, 1242421, 1243421, 
 37 1245421, 1250521, 1253521, 1257521, 1262621, 
 38 1268621, 1273721, 1276721, 1278721, 1280821, 
 39 1281821, 1286821, 1287821, 1300031, 1303031, 
 40 1311131, 1317131, 1327231, 1328231, 1333331, 
 41 1335331, 1338331, 1343431, 1360631, 1362631, 
 42 1363631, 1371731, 1374731, 1390931, 1407041, 
 43 1409041, 1411141, 1412141, 1422241, 1437341, 
 44 1444441, 1447441, 1452541, 1456541, 1461641, 
 45 1463641, 1464641, 1469641, 1486841, 1489841, 
 46 1490941, 1496941, 1508051, 1513151, 1520251, 
 47 1532351, 1535351, 1542451, 1548451, 1550551, 
 48 1551551, 1556551, 1557551, 1565651, 1572751, 
 49 1579751, 1580851, 1583851, 1589851, 1594951, 
 50 1597951, 1598951, 1600061, 1609061, 1611161, 
 51 1616161, 1628261, 1630361, 1633361, 1640461, 
 52 1643461, 1646461, 1654561, 1657561, 1658561, 
 53 1660661, 1670761, 1684861, 1685861, 1688861, 
 54 1695961, 1703071, 1707071, 1712171, 1714171, 
 55 1730371, 1734371, 1737371, 1748471, 1755571, 
 56 1761671, 1764671, 1777771, 1793971, 1802081, 
 57 1805081, 1820281, 1823281, 1824281, 1826281, 
 58 1829281, 1831381, 1832381, 1842481, 1851581, 
 59 1853581, 1856581, 1865681, 1876781, 1878781, 
 60 1879781, 1880881, 1881881, 1883881, 1884881, 
 61 1895981, 1903091, 1908091, 1909091, 1917191, 
 62 1924291, 1930391, 1936391, 1941491, 1951591, 
 63 1952591, 1957591, 1958591, 1963691, 1968691, 
 64 1969691, 1970791, 1976791, 1981891, 1982891, 
 65 1984891, 1987891, 1988891, 1993991, 1995991, 
 66 1998991, 3001003, 3002003, 3007003, 3016103, 
 67 3026203, 3064603, 3065603, 3072703, 3073703, 
 68 3075703, 3083803, 3089803, 3091903, 3095903, 
 69 3103013, 3106013, 3127213, 3135313, 3140413, 
 70 3155513, 3158513, 3160613, 3166613, 3181813, 
 71 3187813, 3193913, 3196913, 3198913, 3211123, 
 72 3212123, 3218123, 3222223, 3223223, 3228223, 
 73 3233323, 3236323, 3241423, 3245423, 3252523, 
 74 3256523, 3258523, 3260623, 3267623, 3272723, 
 75 3283823, 3285823, 3286823, 3288823, 3291923, 
 76 3293923, 3304033, 3305033, 3307033, 3310133, 
 77 3315133, 3319133, 3321233, 3329233, 3331333, 
 78 3337333, 3343433, 3353533, 3362633, 3364633, 
 79 3365633, 3368633, 3380833, 3391933, 3392933, 
 80 3400043, 3411143, 3417143, 3424243, 3425243, 
 81 3427243, 3439343, 3441443, 3443443, 3444443, 
 82 3447443, 3449443, 3452543, 3460643, 3466643, 
 83 3470743, 3479743, 3485843, 3487843, 3503053, 
 84 3515153, 3517153, 3528253, 3541453, 3553553, 
 85 3558553, 3563653, 3569653, 3586853, 3589853, 
 86 3590953, 3591953, 3594953, 3601063, 3607063, 
 87 3618163, 3621263, 3627263, 3635363, 3643463, 
 88 3646463, 3670763, 3673763, 3680863, 3689863, 
 89 3698963, 3708073, 3709073, 3716173, 3717173, 
 90 3721273, 3722273, 3728273, 3732373, 3743473, 
 91 3746473, 3762673, 3763673, 3765673, 3768673, 
 92 3769673, 3773773, 3774773, 3781873, 3784873, 
 93 3792973, 3793973, 3799973, 3804083, 3806083, 
 94 3812183, 3814183, 3826283, 3829283, 3836383, 
 95 3842483, 3853583, 3858583, 3863683, 3864683, 
 96 3867683, 3869683, 3871783, 3878783, 3893983, 
 97 3899983, 3913193, 3916193, 3918193, 3924293, 
 98 3927293, 3931393, 3938393, 3942493, 3946493, 
 99 3948493, 3964693, 3970793, 3983893, 3991993, 
100 3994993, 3997993, 3998993, 7014107, 7035307, 
101 7036307, 7041407, 7046407, 7057507, 7065607, 
102 7069607, 7073707, 7079707, 7082807, 7084807, 
103 7087807, 7093907, 7096907, 7100017, 7114117, 
104 7115117, 7118117, 7129217, 7134317, 7136317, 
105 7141417, 7145417, 7155517, 7156517, 7158517, 
106 7159517, 7177717, 7190917, 7194917, 7215127, 
107 7226227, 7246427, 7249427, 7250527, 7256527, 
108 7257527, 7261627, 7267627, 7276727, 7278727, 
109 7291927, 7300037, 7302037, 7310137, 7314137, 
110 7324237, 7327237, 7347437, 7352537, 7354537, 
111 7362637, 7365637, 7381837, 7388837, 7392937, 
112 7401047, 7403047, 7409047, 7415147, 7434347, 
113 7436347, 7439347, 7452547, 7461647, 7466647, 
114 7472747, 7475747, 7485847, 7486847, 7489847, 
115 7493947, 7507057, 7508057, 7518157, 7519157, 
116 7521257, 7527257, 7540457, 7562657, 7564657, 
117 7576757, 7586857, 7592957, 7594957, 7600067, 
118 7611167, 7619167, 7622267, 7630367, 7632367, 
119 7644467, 7654567, 7662667, 7665667, 7666667, 
120 7668667, 7669667, 7674767, 7681867, 7690967, 
121 7693967, 7696967, 7715177, 7718177, 7722277, 
122 7729277, 7733377, 7742477, 7747477, 7750577, 
123 7758577, 7764677, 7772777, 7774777, 7778777, 
124 7782877, 7783877, 7791977, 7794977, 7807087, 
125 7819187, 7820287, 7821287, 7831387, 7832387, 
126 7838387, 7843487, 7850587, 7856587, 7865687, 
127 7867687, 7868687, 7873787, 7884887, 7891987, 
128 7897987, 7913197, 7916197, 7930397, 7933397, 
129 7935397, 7938397, 7941497, 7943497, 7949497, 
130 7957597, 7958597, 7960697, 7977797, 7984897, 
131 7985897, 7987897, 7996997, 9002009, 9015109, 
132 9024209, 9037309, 9042409, 9043409, 9045409, 
133 9046409, 9049409, 9067609, 9073709, 9076709, 
134 9078709, 9091909, 9095909, 9103019, 9109019, 
135 9110119, 9127219, 9128219, 9136319, 9149419, 
136 9169619, 9173719, 9174719, 9179719, 9185819, 
137 9196919, 9199919, 9200029, 9209029, 9212129, 
138 9217129, 9222229, 9223229, 9230329, 9231329, 
139 9255529, 9269629, 9271729, 9277729, 9280829, 
140 9286829, 9289829, 9318139, 9320239, 9324239, 
141 9329239, 9332339, 9338339, 9351539, 9357539, 
142 9375739, 9384839, 9397939, 9400049, 9414149, 
143 9419149, 9433349, 9439349, 9440449, 9446449, 
144 9451549, 9470749, 9477749, 9492949, 9493949, 
145 9495949, 9504059, 9514159, 9526259, 9529259, 
146 9547459, 9556559, 9558559, 9561659, 9577759, 
147 9583859, 9585859, 9586859, 9601069, 9602069, 
148 9604069, 9610169, 9620269, 9624269, 9626269, 
149 9632369, 9634369, 9645469, 9650569, 9657569, 
150 9670769, 9686869, 9700079, 9709079, 9711179, 
151 9714179, 9724279, 9727279, 9732379, 9733379, 
152 9743479, 9749479, 9752579, 9754579, 9758579, 
153 9762679, 9770779, 9776779, 9779779, 9781879, 
154 9782879, 9787879, 9788879, 9795979, 9801089, 
155 9807089, 9809089, 9817189, 9818189, 9820289, 
156 9822289, 9836389, 9837389, 9845489, 9852589, 
157 9871789, 9888889, 9889889, 9896989, 9902099, 
158 9907099, 9908099, 9916199, 9918199, 9919199, 
159 9921299, 9923299, 9926299, 9927299, 9931399, 
160 9932399, 9935399, 9938399, 9957599, 9965699, 
161 9978799, 9980899, 9981899, 9989899
162 };
163 
164 int main()
165 {
166     int a, b, i;
167     while(scanf("%d %d", &a, &b)!=EOF)
168     {
169         for(i=0;i<N;i++)
170             if(table[i]>=a&&table[i]<=b)
171                 printf("%d\n", table[i]);
172         printf("\n");
173     }
174     return 0;
175 }
AC

标签:10,int,flag,4N,素数,include,回文
来源: https://www.cnblogs.com/goldenretriever/p/10356168.html