Use the Sieve of Eeratosthenes in a certain range and the number of negative numbers (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]

——————- ——————— ————————————————– ——————

In fact, prime numbers do not include 1, so 1 is excluded.

The Sieve of Eratosthenes is used to filter prime numbers in the sequence of natural numbers.

Emirp: A number is a prime number and vice versa, such as 13 and 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< /span> sieveOfEratosthenes(int *n, int number);
14 void emirp(int *n, int number);
< /span>15 int reverse(int< /span> 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 ){
23 primes[i] = 1;
24 }
25 /* Write a prime checking algorithm here. */< br />26 sieveOfEratosthenes(primes, PRIMECOUNT);
27 //printf("%d ", primes[ 6]);
28
29 emirp(primes, PRIMECOUNT);
30 //printf("%d ", primes[6]);
31 /* ------------------ -------------------- */
32 printf("All primes found from 1-1000: ");
33 for(i = 0; i ){
34 if(primes[i] == 1){
35 printf("%d ",i + 1);
36 }
37 }
38 printf(" ");
39 return 0;
40 }
41
42 void sieveOfEratosthenes(int *n, int number) {
43 int i = 0, j = 0, k = 2;
44< /span> n[0] = 0 ;
45 for (i = 1; i ) {
46< /span> if (n[i] == 0) {
47 continue;
48 }
49 for
(j = i, k = 2; (j + 1) * k <= number; k++) {
50< /span> n[(j + 1) * k-1] = 0;
51 }
52 }
53 }
54
55 void emirp(int *n, int number) {
56 int i = 0;
< span style="color: #008080">57
for (i = 0 ; i ) {
58 if (n[i] == 1 && n[reverse(i + 1)-1] == 0) {
59 //printf("%d is not! ", i+1);
60 n[ i] = 0;
61 //< span style="color: #008000">n[reverse(i + 1)-1] = 0;

62 }
63 }
64 }
65
66 int reverse(int n) {
67 int reverseNumber = 0, temp = 0;
68 while< /span> (n != 0) {
69 temp = n% < span style="color: #800080">10
;
70 n = n / 10;
71 reverseNumber = reverseNumber * 10 + temp;
< span style="color: #008080">72 }
73 return reverseNumber;
74 }

The mistake made was to judge whether the 55 line is an inverse prime number, which was originally

1 void emirp(int *n, int number) {
2 int i = 0;
3 for (i = 0; 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 }

This will make things like 2, 11, 31, etc. Anti-prime numbers are excluded because 20, 110, and 310 are not prime numbers.

 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 #inclu de
9 #include
10< /span>
11 #define PRIMECOUNT 1000
12
13 void sieveOfEratosthenes( int *n, int number);< br />14 void emirp(int *n, int number);
15 int reverse(int n);
16
17 int main(int argc, char **< span style="color: #000000">argv){
18 /* The primes 1-1000, if n is prime,
19 primes[n-1] == 1 and 0 otherwise. */
< span style="color: #008080">20 int primes[PRIMECOUNT];
21 int i;
22 for(i = 0; i ){
23 primes[ i] = 1;
24 }
25 /* Write a prime checking algorithm here. */
26 sieveOfEratosthenes(primes, PRIMECOUNT);
27 //printf("%d ", primes[6] );
28
29 emirp(primes, PRIMECOUNT);
30 / /printf("%d ", primes[6]);
31 /*
----------------------- --------------- */
32 printf("All primes found from 1-1000: < span style="color: #800000">");
33 span> for(i = 0; i ){
34 if(primes[i ] == 1){
35 printf("%d ",i + 1);
36 }
37 }
38 printf (" " );
39 return< /span> 0;
40 }
41
42 void sieveOfEratosthenes(int *n, int number) {
43 in t i = 0, j = 0, k = 2;
44 n[< span style="color: #800080">0
] = 0;
< /span>45 for (i = 1 ; i ) {
46 if (n[i] == 0) {< br />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 span>
55 void emirp(int *n, int number) {
56 int i = 0;
57 for (i = 0; i ) {
58 if (n[i] == 1 && n[reverse(i + 1)-1] == 0) {
59 //printf("%d is not! ", i+1);
60 n[i] = < span style="color: #800080">0;
61 //n[reverse(i + 1)-1] = 0;
62 }
63 }
64 }
65 span>
66 int reverse(int n) {
67 int reverseNumber = 0, temp = 0;
68 while ( n != 0) {
69 temp = n% 10;
70 n = n / 10;
71< /span> reverseNumber = reverseNumber * 10 + temp;
72 }
73 return reverseNumber;
74 }

1 void emirp(< span style="color: #0000ff">int *n, int number) {
2 int i = 0;
3 for (i = 0; i ) {
4 if (!(n[i] == 1 && n[reverse(i + 1)-1] == 1)) {
5 span> n[i] = 0;
6 n[reverse(i + 1)-1] = 0;
7< /span> }
8
}
9 }

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3991 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.