user-0 提问: （C语言）编程实现，将1-1000间的所有超级素数显示出来。超级素数是各个位的数字都是素数，且本身也为素数的数。

回复:
user: user-1
content: 简单一点：  #include <stdio.h>  int main() {   int i, j, k, p[168] = {2, 3, 5, 7};    for (i = 11, k = 4; j = 1, i < 1000; i += 2)   {     while (j < k && p[j] < 32) if (!(i % p[j++])) break;     if (j == k || i % p[j - 1]) p[k++] = i;   }    for (i = 0; j = p[i] / 100, k = p[i] % 100 / 10, i < 168; i++)     if (p[i] < 9 || ((p[i] % 10 == 3 || p[i] % 10 == 7) &&                        (!j || j == 2 || j == 3 || j == 5 || j == 7) &&                         (k == 2 || k == 3 || k == 5 || k == 7)))       printf("%d\n", p[i]);    return 0; }

user: user-2
content: int main() {         int n[]={0,2,3,5,7};         int i,j,k,t;         for(i=0;i<5;i++)                 for(j=(i?1:0);j<5;j++)                         for(k=1;k<5;k++)                                 if(IsPrimeLT1k(t=100*n[i]+10*n[j]+n[k]))                                         printf("%d\n",t);         return 0; }

user: user-2
content: 在限定范围的情况下，要追求快可以离线制表，O(1)： int IsPrimeLT1k(int a) {         static unsigned int x[]={2693408940,673221152,34112136,2147625122,                 547359240,2150113416,2148008098,134382120,169910402,579338272,                 671221760,2283831426,33693728,142770216,2148049410,545785984,                 536873472,168304648,2189559810,8391200,2629770,537397282,                 545292320,35684488,537395234,681576960,2317353088,9052160,                 8423424,34087426,2156005504,673316896 };         return x[a/32]&(1<<(a%32)); }

user: user-3
content: 引用坛子里怎么都跟素数干上了，下面是我上午刚写的函数 #5楼 算法是最节约时间的

user: user-4
content: 引用 4 楼 user-6 的回复:上面代码，漏掉一个质数2。进一步修正如下： #include <stdio.h> #include <math.h>   int is_prime(int num);   int main(void) {     int i, tmp;          for (i = 1; i < 1000; i++) {         tmp = i;         if (is_prime(tmp)) {             while (is_prime(tmp % 10)) tmp /= 10;             if (tmp == 0) printf("%d ", i);         }     }       return 0; }   int is_prime(int num) {     int i, size;       if (num < 2) return 0;       size = (int)sqrt(num);     for (i = 2; i < size + 1; i++)         if (num % i == 0) return 0;       return 1; } 对于小于10的素数是不是也要进入while循环？

user: user-5
content: 坛子里怎么都跟素数干上了，下面是我上午刚写的函数   bool IsPrime(int n) { 	if (n == 1) 		return false;  	if (n == 2 		|| n == 3 		|| n == 5 		|| n == 7) 	{ 		return true; 	}  	if (n % 2 == 0) 		return false;  	if (n % 3 == 0) 		return false;  	if (n % 5 == 0) 		return false;  	if (n % 7 == 0) 		return false;  	double dn = sqrt(n); 	int cnt = (int)dn; 	for (int i = 11; i <= cnt; i += 2) 	{ 		if (n % i == 0) 			return false; 	}  	return true; }

user: user-6
content: 上面代码，漏掉一个质数2。进一步修正如下： #include <stdio.h> #include <math.h>   int is_prime(int num);   int main(void) {     int i, tmp;          for (i = 1; i < 1000; i++) {         tmp = i;         if (is_prime(tmp)) {             while (is_prime(tmp % 10)) tmp /= 10;             if (tmp == 0) printf("%d ", i);         }     }       return 0; }   int is_prime(int num) {     int i, size;       if (num < 2) return 0;       size = (int)sqrt(num);     for (i = 2; i < size + 1; i++)         if (num % i == 0) return 0;       return 1; }

user: user-6
content: 把2楼的main函数简化一下，如下，供参考： #include <stdio.h> #include <math.h>   int is_prime(int num);   int main(void) {     int i, tmp;          for (i = 1; i < 1000; i += 2) {         tmp = i;         if (is_prime(tmp)) {             while (is_prime(tmp % 10)) tmp /= 10;             if (tmp == 0) printf("%d ", i);         }     }       return 0; }   int is_prime(int num) {     int i, size;       if (num < 2) return 0;       size = (int)sqrt(num);     for (i = 2; i < size + 1; i++)         if (num % i == 0) return 0;       return 1; }

user: user-4
content: #include <stdio.h> #include <math.h>  int is_prime(int num);  int main(void) {      int i;     int tmp;      for (i = 1; i < 1000; i += 2) {         //printf("i = %d\n", i);         tmp = i;         if (is_prime(tmp)) {             if (tmp < 10) {                 printf("%d ", i);                 continue;             }             while (tmp) {                 if (is_prime(tmp % 10)) {                     tmp /= 10;                     continue;                 }                 break;             }             if (tmp == 0)                 printf("%d ", i);         }     }      return 0;  }  int is_prime(int num) {     int i;      if (num < 2)         return 0;      int size = (int)sqrt(num);     for (i = 2; i < size + 1; i++)         if (num % i == 0)             return 0;      return 1; }  供参考～