c - What is the best way to generate prime numbers? -


i doing exercises programming in c kochan; @ initial stage, chapter 5.

here task:

program 5.10 has several inefficiencies. 1 inefficiency results checking numbers. because obvious number greater 2 cannot prime, program skip numbers possible primes , possible divisors. inner loop inefficient because value of p divided values of d 2 through. inefficiency avoided adding test value of is_prime in conditions of loop. in manner, loop set continue long no divisor found , value of d less p. modify program 5.10 incorporate these 2 changes.

here program 5.10

    // generate table of prime numbers     #include <stdio.h>      int main (void)     {         int p, d;         _bool is_prime;          (p = 2; p <= 50; ++p)         {             is_prime = 1;              (d = 2; d < p; ++d)                 if (p % d == 0)                     is_prime = 0;                  if (is_prime)          // or if (is_prime != 0); same                     printf ("%i ", p);         }          printf ("\n");         return 0;     } 

here 2 options trying write, both print out blank space; no numbers produced. first 1 might represent wrong approach, can't see why second wouldn't work.

option 1:

 // generate table of prime numbers #include <stdio.h> #include <stdbool.h>  int main (void) {     int p, d;     bool is_prime;      /* start p=2, until p less 50,       , skip numbers */      (p = 2; (p < 50) && (p % 2 != 0); ++p)      {         is_prime =1;          /* inner loop says: start d = 2; until          d less p, , test if p prime or not          dividing p d */          (d = 2; d < p (p % d != 0 ? is_prime : !is_prime); ++d)               printf ("%i ", p);     }      printf ("\n");     return 0; } 

option 2:

// generate table of prime numbers #include <stdio.h> #include <stdbool.h>  int main (void) {     int p, d;     bool is_prime;      /* start p=2, until p less 50,       , skip numbers */      (p = 2; (p < 50) && (p % 2 != 0); ++p)      {         is_prime =1;          /* inner loop says: start d = 2; until          d less p, , test if p prime or not          dividing p d */          (d = 2; (d < p) && (p % d != 0); ++d )             /* inner loop supposed print number if              not divided d */              printf ("%i ", p);     }      printf ("\n");     return 0; } 

i grateful help! new programming.

thank you!

since still learning, consider breaking down pieces, example creating isprime function, e.g.:

int isprime(int num) {     int i;     (i = 2; < num; i++) {         if (num % == 0 && != num)              return 0;     }     return 1; } 

then, can use approaches, e.g.:

#include <stdio.h>  int main() {     int p, d;      /* avoid (p == 2) comparison each iteration */     printf ("2 ");      /* go 3 49 , print prime numbers without checking numbers */     (p = 3; p < 50; ++p) {         if (p % 2 != 0){             if (isprime(p))                 printf ("%i ", p);         }     }      return 0; } 

of course there many different ways , should consider using time library (perhaps time.h) performance evaluation.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -