Advertisement
EmanueleBonin

Crivello di Eratostene con calcolo pi

Jul 10th, 2019
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. #define RANGE 10000u
  5.  
  6. typedef unsigned char uint_8;
  7.  
  8. uint_8 composite[RANGE];
  9.  
  10. int main(void) {
  11.     clock_t Ticks, TicksDelta;
  12.     unsigned int Sec;
  13.     unsigned int Milli;
  14.  
  15.     unsigned int i;
  16.     unsigned int j=2;
  17.     unsigned int pi=0;
  18.     unsigned int pr=0;
  19.     unsigned int ScopeMax;
  20.     unsigned int ScopeMaxMax;
  21.  
  22.  
  23.     for(j=0; j<RANGE; ++j) {
  24.         composite[j]=0;
  25.     }
  26.     ScopeMax    = RANGE/2;
  27.     ScopeMaxMax= ScopeMax;
  28.  
  29.     printf("\nPress enter to start\n");
  30.     getchar();
  31.     printf("\nComputing...\n");
  32.     Ticks = clock();
  33.     j=2;
  34.     while(j<ScopeMaxMax) {
  35.     pr=j;  
  36.         for (i=2; i<=ScopeMax; ++i) { pr=pr+j; composite[pr-1]=1;}
  37.      
  38.         ++j;
  39.         ScopeMax=RANGE/j;
  40.      }
  41.     TicksDelta = clock() - Ticks;
  42.     Sec = (unsigned short) (TicksDelta / CLOCKS_PER_SEC);
  43.     Milli = ((TicksDelta % CLOCKS_PER_SEC) * 1000) / CLOCKS_PER_SEC;
  44.     printf ("Time used: %u.%03u seconds = %u ticks\n", Sec, Milli, (unsigned short) TicksDelta);
  45.  
  46.     printf("Press enter to print the prime numbers\n");
  47.     getchar();
  48.     printf("\n");
  49.     composite[0]=1;
  50.     composite[1]=0;
  51.     for(i=0; i<RANGE; ++i) {
  52.         if(composite[i]==0) {
  53.         ++pi;
  54.             printf(" %u ", i+1);
  55.         }
  56.     }
  57.     printf("\nPi = %u", pi);
  58.  
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement