Computer Programming Contest Preparation

ToolBox - Source for: 107/10789/prime_num_generator.cpp



/home/toolbox/public_html/solutions/107/10789/prime_num_generator.cpp
    1 #include <iostream>
    2 #include <vector>
    3 
    4 const long int SIZE = 1000000;
    5 
    6 int numbers[SIZE];
    7 std::vector<int> primes;
    8 
    9 int main(void)
   10 {
   11     for(int x = 0; x < SIZE; x++)
   12         {
   13             numbers[x] = x;
   14         }
   15     numbers[1] = 0;
   16     for(long int x = 2; x < SIZE; x++)
   17         {
   18             for(long int y = x; x*y < SIZE && x*y >= 0; y++)
   19                 {
   20                     numbers[(long long)x*y] = 0;
   21                 }
   22         }
   23     for(int x = 0; x < SIZE; x++)
   24         {
   25             if(numbers[x] != 0)
   26                 {
   27                     primes.push_back(numbers[x]);
   28                 }
   29         }
   30     std::vector<int>::iterator it = primes.begin();
   31     std::vector<int>::iterator end = primes.end();
   32     std::cerr << "int primes [" << primes.size() << "] = { ";
   33     while(it != end)
   34         {
   35             std::cerr << *it << ", ";
   36             ++it;
   37         }
   38 }
   39