Skip to content

Instantly share code, notes, and snippets.

@abdelaziz321
Last active February 16, 2018 12:00
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 abdelaziz321/0ed96cfa612606a0d4e7593010206526 to your computer and use it in GitHub Desktop.
Save abdelaziz321/0ed96cfa612606a0d4e7593010206526 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
bool X[1001];
using namespace std;
int main()
{
memset(X,true,sizeof(X));
X[0] = false;
for (int i=2; i<1001; i++) { // creating the prime list
if (X[i] == false) continue;
for (int j=2; i*j<=1001; j++) {
X[i*j] = false;
}
}
int n,c;
while (cin >> n >> c) {
// creating the n list
vector<int> nList;
vector<int>::iterator it;
for (int i=1; i<n+1; i++) {
if (X[i] == true) {
nList.push_back(i);
}
}
int nListLen = nList.size();
// the length of the center list
int centerListLen;
if (nListLen%2 == 0) { // even
centerListLen = 2 * c;
} else { // odd
centerListLen = 2 * c - 1;
}
cout << n << " " << c << ":";
if (centerListLen >= nListLen) { // the center list exceeds the limits of the n list
for (it=nList.begin(); it!=nList.end(); it++) {
cout << " " << *it;
}
} else {
// output the center list
int indent = (nListLen - centerListLen) / 2;
for(int i=indent; i<nListLen-indent; i++) {
cout << " " << nList.at(i);
}
}
cout << endl << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment