1. Sieve of Erathosthenes
    In mathematics, the Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer. It works efficiently for the smaller primes (below 10 million) . It was created by Eratosthenes, an ancient Greek mathematician.

  2. The Algorithm
    1. Create a contiguous list of numbers from two to some highest number n.
    2. Strike out from the list all multiples of two (4, 6, 8 etc.).
    3. The list's next number that has not been struck out is a prime number.
    4. Strike out from the list all multiples of the number you identified in the previous step.
    5. Repeat steps 3 and 4 until you reach a number that is greater than the square root of n (the highest number in the list).
    6. All the remaining numbers in the list are prime.


  3. Output
    25 Primes to 100
        2    3    5    7   11
       13   17   19   23   29
       31   37   41   43   47
       53   59   61   67   71
       73   79   83   89   97
    
    Twin Primes
    3       5
    5       7
    11      13
    17      19
    29      31
    41      43
    59      61
    71      73
    Press any key to continue . . .
    

  4. The code
    //
    // Sieve of Erathosthenes
    //
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    int main ()
    {
       const int TO = 100;
       int sieve[TO + 1];   // Just ignore sieve[0]
       int index;
       int count;
      
       //
       // first fill the sieve
       //
       for(index=0; index < TO + 1; index++) sieve[index]=index;
      
       //
       // start marking out non primes
       // start at 2
       for(index=2; index <= sqrt(TO); index++) {
          if(! sieve[index]) continue;   // already marked out
          for(int inner=index+index; inner < TO + 1; inner+=index)
             sieve[inner]=0; // mark out all multiples of index
       }
       //
       // count the primes
       //
       count=0;
       for(index=2; index < TO +1; index++)
        if(sieve[index]) count++;
       //
       // print the primes
       //
       cout << count << " Primes to " << TO << endl;
       count=0;
       for(index=2; index < TO +1; index++)
          if(sieve[index]) {
            cout << setw (5) << sieve[index];
            if(!(++count % 5)) cout << endl;
          }
       //
       // print the twin primes
       //
       cout << endl <<  "Twin Primes " << endl;
       for(index=2; index< TO; index++)
         if(sieve[index] && sieve[index+2])
          cout << sieve[index] << "\t" 
               << sieve[index+2] << endl;
             
       system("pause");
       return 0;
    }