home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104084a < prev    next >
Text File  |  1992-12-23  |  931b  |  32 lines

  1. /* primes.c */
  2. /* Copyright 1992 by P.J. LaBrocca */
  3.  
  4. #include <stdlib.h>
  5. #include "mixed.h"
  6.  
  7. #define NUM 16000     /* highest number to check for being prime */
  8.  
  9. Integer Primes[ 2000 ];
  10.  
  11. void init_primes( void )
  12. {
  13.     char *mark = malloc(NUM * sizeof(char) );
  14.     Integer *pr = Primes;  /* point to global array */
  15.     Integer j, k;
  16.  
  17.     for(j = 0; j < NUM; ++j)
  18.         mark[j] = 1;     /* mark everything prime */
  19.     for(j = 4; j < NUM; j += 2)
  20.         mark[j] = 0;     /* scratch off all the even numbers ... */
  21.     *pr++ = 2;           /* ... except for 2; put it in primes array */
  22.     for(j = 3; j < NUM; j += 2)  /* check each odd number: */
  23.         if(mark[j]) {            /* if it's marked... */
  24.             *pr++ = j;           /* ..record it in array.. */
  25.             for(k = j + j; k < NUM; k += j)
  26.                 mark[k] = 0;     /* ..and scratch off all its multiples */
  27.         }
  28.     free( mark );
  29. }
  30. #undef NUM
  31.  
  32.