Advertisement
sebbu

eratosthenes

May 23rd, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. #include <vector>
  2. #include <bitset>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. template<int SIZE>
  8. vector<int> eratosthenes()
  9. {
  10.     bitset<SIZE> sieve;
  11.     vector<int> primes;
  12.     sieve.set();
  13.  
  14.     for(int i=2; i<SIZE; i++)
  15.         if (sieve[i]) {
  16.             primes.push_back(i);
  17.             for (int j = i*i; j<SIZE; j+=i)
  18.                 sieve[j] = false;
  19.         }
  20.     return primes;
  21. }
  22.  
  23. int main(void)
  24. {
  25.     vector<int> era = eratosthenes<100>();
  26.     for(unsigned int i=0;i<era.size();i++)
  27.     {
  28.         printf("%d\n", era[i]);
  29.     }
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement