Skip to content

Instantly share code, notes, and snippets.

@xywei
Last active September 6, 2016 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xywei/125a2a9159ee24e3e1b3beb0c5589c2e to your computer and use it in GitHub Desktop.
Save xywei/125a2a9159ee24e3e1b3beb0c5589c2e to your computer and use it in GitHub Desktop.
Simple Prime Number Generator
// 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);
}
}
}
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