下面是代码示例:
- /*
- ID: alogfan1
- LANG: C
- TASK: sprime
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- int powi(int i, int n)
- {
- int t, result = 1;
- for (t = 1; t <= n; t++)
- result *= i;
- return result;
- }
- int isspecprime(int i)
- {
- int tmp = i;
- while(tmp != 0)
- {
- if (!isprime(tmp))
- return 0;
- tmp /= 10;
- }
- return 1;
- }
- int isprime(int n)
- {
- if (n == 1)
- return 0;
- int i, k = (int)sqrt(n);
- for(i = 2; i <= k; i++)
- if(n % i == 0)
- return 0;
- return 1;
- }
- int main()
- {
- #ifndef __LOCAL_TEST__
- freopen("sprime.in", "r", stdin);
- freopen("sprime.out", "w", stdout);
- #endif // LOCAL
- int bits;
- scanf("%d", &bits);
- int sheets[1024] = {2, 3, 5, 7};
- int source[] = {1, 3, 5, 7, 9};
- int beg = 0, len = 4;
- int i;
- for (i = 1; i < bits; i++)
- {
- int _len = 0;
- int m;
- for (m = beg; m < beg + len; m++)
- {
- int j;
- for (j = 0; j < 5; j++)
- {
- int tmp = source[j] + sheets[m] * 10;
- if (isprime(tmp))
- {
- sheets[beg + len + _len] = tmp;
- _len++;
- }
- }
- }
- beg = beg + len;
- len = _len;
- }
- for (i = beg; i < beg + len; i++)
- {
- printf("%d\n", sheets[i]);
- }
- return 0;
- }