home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / SIEVE.CPP < prev    next >
C/C++ Source or Header  |  1997-02-14  |  937b  |  37 lines

  1. /**************************************************************************
  2.  *
  3.  * sieve.cpp -     sieve program.  Section 5.3
  4.  *
  5.  * $Id: sieve.cpp,v 1.3 1995/08/29 19:03:22 oberg Exp $
  6.  *
  7.  * $$RW_INSERT_HEADER "slyrs.cpp"
  8.  *
  9.  **************************************************************************/
  10.  
  11. # include <vector>
  12. # include <iostream.h>
  13. using namespace std;
  14.  
  15. int main() {
  16.     cout << "Prime Sieve program, a test of vectors" << endl;
  17.     
  18.     // create a sieve of bits, initially on
  19.     const int sievesize = 100;
  20.     vector<int> sieve(sievesize, 1);
  21.         
  22.         // now search for 1 bt positions
  23.     for (int i = 2; i * i < sievesize; i++)
  24.         if (sieve[i])
  25.             for (int j = i + i; j < sievesize; j += i)
  26.                 sieve[j] = 0;
  27.                     
  28.         // now output all the values that are set
  29.     for (int j = 2; j < sievesize; j++)
  30.         if (sieve[j])
  31.             cout << j << " ";
  32.     cout << endl;
  33.     
  34.     cout << "End of Prime Sieve program" << endl;
  35.     return 0;
  36. }
  37.