Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created September 25, 2016 16:24
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 Garciat/fdd4203dd744c28dbb3f897191a31c94 to your computer and use it in GitHub Desktop.
Save Garciat/fdd4203dd744c28dbb3f897191a31c94 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
using namespace std;
std::string exec(const string &cmd) {
char buffer[128];
std::string result = "";
std::shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer, 128, pipe.get()) != NULL)
result += buffer;
}
return result;
}
int main() {
const string pre = "factor ";
string line;
getline(cin, line); // skip count line
while (getline(cin, line)) {
bool prime = exec(pre + line).size() == (2 * line.size() + 3);
cout << (prime ? "Prime" : "Not prime") << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment