使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp)

Programming 1.3 In this problem, you'll be asked to find all the prime numbers from 1 to 1000. Prime numbers are used in all
kinds of circumstances, particularly in fields such as cryptography, hashing among many others. Any method w ill be sufficient for
this problem. The Sieve of Eratosthenes is one algorithm which you can try implementing, but there are plenty of others. A prime is
prime if it is not the product of any lesser natural numbers except for 1 and itself, for example, 1, 2 and 3 should be prime by this
definition, but 4, being the product of 2 and 2, is not.

Programming Challenge 1.1 Prime numbers are fairly useful on their own, but another fun challenge is to find what are known
as emirp primes. Emirp primes are primes which are also primes when reversed. For this challenge, you need to print out the list
of all primes that are also primes (as defined in the challenge above) when reversed. [2]

------------------------------------------------------------------------------------------------------------

实际上素数(prime number)是不包括1的,所以就把1排除掉了。

埃拉托色尼筛选法(the Sieve of Eratosthenes)用于筛选自然数数列中的素数。

反素数(Emirp):一个数是素数且将他反过来也是素数,如13和31。

 1 /* Prime numbers are fairly useful on their ow n, 
 2 but another fun challenge is to find w hat are know n
 3 as emirp primes. Emirp primes are primes w hich are also 
 4 primes w hen reversed. For this challenge, you need to 
 5 print out the list of all primes that are also primes 
 6 (as defined in the challenge above) w hen reversed from 1 - 1000.
 7 */
 8 #include 
 9 #include 
10 
11 #define PRIMECOUNT 1000
12 
13 void sieveOfEratosthenes(int *n, int number);
14 void emirp(int *n, int number);
15 int reverse(int n);
16 
17 int main(int argc, char **argv){
18     /* The primes 1 - 1000, if n is prime,
19     primes[n - 1] == 1 and 0 otherwise. */
20     int primes[PRIMECOUNT];
21     int i;
22     for(i = 0; i < PRIMECOUNT; i++){
23         primes[i] = 1;
24     }
25     /* Write a prime checking algorithm here. */
26     sieveOfEratosthenes(primes, PRIMECOUNT);
27     //printf("%d\n", primes[6]);
28     
29     emirp(primes, PRIMECOUNT);
30     //printf("%d\n", primes[6]);
31     /* -------------------------------------- */
32     printf("All primes found from 1 - 1000:\n");
33     for(i = 0; i < PRIMECOUNT; i++){
34         if(primes[i] == 1){
35         printf("%d\t",i + 1);
36         }
37     }
38     printf("\n");
39     return 0;
40 }
41 
42 void sieveOfEratosthenes(int *n, int number) {
43     int i = 0, j = 0, k = 2;
44     n[0] = 0;
45     for (i = 1; i < number; i++) {
46         if (n[i] == 0) {
47             continue;
48         }
49         for (j = i, k = 2; (j + 1) * k <= number; k++) {
50             n[(j + 1) * k - 1] = 0;
51         }
52     }
53 }
54 
55 void emirp(int *n, int number) {
56     int i = 0;
57     for (i = 0; i < number; i++) {
58         if (n[i] == 1 && n[reverse(i + 1) - 1] == 0) {
59             //printf("%d is not!\n", i+1);
60             n[i] = 0;
61             //n[reverse(i + 1) - 1] = 0;
62         }
63     }
64 }
65 
66 int reverse(int n) {
67     int reverseNumber = 0, temp = 0;
68     while (n != 0) {
69         temp = n % 10;
70         n = n / 10;
71         reverseNumber = reverseNumber * 10 + temp;
72     }
73     return reverseNumber;
74 }

所犯的错误是55行的判断是否是反素数,原先是

1 void emirp(int *n, int number) {
2     int i = 0;
3     for (i = 0; i < number; i++) {
4         if (!(n[i] == 1 && n[reverse(i + 1) - 1] == 1)) {
5             n[i] = 0;
6             n[reverse(i + 1) - 1] = 0;
7         }
8     }
9 }

这样会使得如2,11,31等被排除反素数,因为20,110,310不是素数。

你可能感兴趣的:(使用埃拉托色尼筛选法(the Sieve of Eratosthenes)在一定范围内求素数及反素数(Emirp))