Last active
September 6, 2016 01:45
-
-
Save xywei/125a2a9159ee24e3e1b3beb0c5589c2e to your computer and use it in GitHub Desktop.
Simple Prime Number Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include STL | |
#include <iostream> | |
#include <bitset> | |
#include <vector> | |
using namespace std; | |
template<int M> | |
void prime_list (vector<int> & list) { | |
list.clear(); | |
bitset<M> s; | |
int head = 2; | |
while (head < M) { | |
int p = head; | |
while (p < M) { | |
s[p] = 1; | |
p += head; | |
} | |
s[head] = 0; | |
head++; | |
while (head < M && s[head]==1) { | |
head++; | |
} | |
} | |
for(int i=2; i<M; i++) { | |
if(s[i]==0) { | |
list.push_back(i); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char ** argv) { | |
constexpr int MAXP = 1000 * 1000; | |
vector<int> pl; | |
prime_list<MAXP> (pl); | |
for (int i=0; i<pl.size(); i++) { | |
cout << pl[i] << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment