This is my submission for the Prime Generator on SPOJ and it was accepted. Are there any improvements/changes I can make?
Input:
The input begins with the number \$t\$ of test cases in a single line (\$t \le 10\$). In each of the next t lines there are two numbers \$m\$ and \$n\$ (\$1 \le m \le n \le 1000000000\$, \$n-m \le 100000\$) separated by a space.
Output:
For every test case print all prime numbers \$p\$ such that \$m \le p \le n\$, one number per line, test cases separated by an empty line.
Example:
Input:
2 1 10 3 5
Output:
2 3 5 7 3 5
#include <iostream>
#include <cmath>
#include <vector>
using std::vector;
using std::cout;
using std::cin;
bool isPrime(int n) {
if (n == 1) return false;
if (n == 2) return true; // invariant
int root = int(ceil(sqrt(n))); // check up to ceil of square root n
for (int i = 2; i <= root; ++i){
if (n % i == 0) return false; // not prime
}
return true;
}
int main() {
int lines;
int toPush;
vector<int> inputs;
// get inputs
cin >> lines;
int tempA = 0;
while (tempA < 2*lines) {
cin >> toPush;
inputs.push_back(toPush);
++tempA;
}
auto i = inputs.begin();
while (i < inputs.end()){
int m = *i;
int n = *(i + 1);
while (m <= n){
if (isPrime(m)){
int value = m;
cout << value << " ";
}
++m;
}
cout << "\n";
std::advance(i, 2);
}
}