Advertisement
EmanueleBonin

YAES - Yet Another Eratostene Sieve

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