home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 146_01 / prime.c < prev    next >
Text File  |  1985-03-10  |  2KB  |  80 lines

  1. /*
  2. HEADER:       CUG146.05;
  3. TITLE:        Small C compiler for 6800;
  4. DESCRIPTION:  "Erathosthenes Sieve Prime Number Program in C
  5.               as excerpted from the Sept, 1981 issue of BYTE
  6.               magazine. Modified for compatibility with Small-C
  7.               derivatives, which do not have FOR loops. As
  8.               published in '68' MICRO Journal, July 1982.";
  9. KEYWORDS:     prime number program in C;
  10. FILENAME:     prime.c
  11. */
  12. /* Erathosthenes Sieve Prime Number Program in C
  13.  *
  14.  * As excerpted from the Sept, 1981 issue of BYTE
  15.  * magazine.
  16.  *
  17.  * Modified for compatibility with Small-C deriva-
  18.  * tives, which do not have FOR loops.
  19.  *
  20.  * As published in '68' MICRO Journal, July 1982.
  21.  *
  22.  */
  23.  
  24. #define TRUE    1
  25. #define FALSE   0
  26. #define SIZE    8190
  27. #define SIZEP1  8191
  28.  
  29. char flags[SIZEP1];
  30.  
  31. main()
  32. {
  33.         int i, prime, k, count, iter;
  34.  
  35.         puts ("10 iterations"); nl();
  36.         iter=1;
  37.         while (iter <= 10)
  38.                 {
  39.                 count=0;
  40.                 i=0;
  41.                 while (i <= SIZE)
  42.                         {
  43.                         flags[i]=TRUE;
  44.                         i++;
  45.                         }
  46.                 i=0;
  47.                 while (i <= SIZE)
  48.                         {
  49.                         if (flags[i])
  50.                                 {
  51.                                 prime=i+i+3;
  52.                                 k=i+prime;
  53.                                 while (k <= SIZE)
  54.                                         {
  55.                                         flags[k]=FALSE;
  56.                                         k=k+prime;
  57.                                         }
  58.                                 count++;
  59.                                 }
  60.                         i++;
  61.                         }
  62.                 iter++;
  63.                 }
  64.                 outdec (count);
  65.                 puts(" primes"); nl();
  66.         }
  67.  
  68. nl()
  69. {
  70.         putchar (13);
  71.         }
  72.  
  73. outdec (n)
  74. int n;
  75. {
  76.         if (n > 9)
  77.                 outdec (n/10);
  78.         putchar ('0'+(n%10));
  79.         }
  80. SCRIPTION:  "Erathosthenes Sieve Prime Number Program