Skip to content

Instantly share code, notes, and snippets.

@xyos
Created October 6, 2011 17:49
Show Gist options
  • Save xyos/1268087 to your computer and use it in GitHub Desktop.
Save xyos/1268087 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> myprimes;
vector<int>::iterator it;
int prime(int n) {
it = find (myprimes.begin(), myprimes.end(), n);
if (it != myprimes.end()) {
return 1;
}
if(n == 2){
myprimes.push_back(n);
return 1;
}
int i;
i = 2;
if (n % i == 0) return 0;
for (i=3; (i*i) <= n; i+=2) {
if (n % i == 0) return 0;
}
myprimes.push_back(n);
return 1;
}
vector<int> primes(int min, int max){
vector<int> p;
int ini = (min < 2) ? 2 : min;
for(int t = ini; t <= max; t++){
if(prime(t))
p.push_back(t);
}
return p;
}
int pv(vector<int> v){
for(vector<int>::size_type j = 0; j != v.size() - 1 ; j++) {
cout << v[j] << endl;
}
cout << v[v.size() - 1];
return 0;
}
int main()
{
int n, min, max;
int i = 0;
cin >> n;
vector< vector<int> > x;
x.resize(n);
while(n--){
scanf("%d %d", &min, &max);
x[i++] = primes(min, max);
}
for(vector< vector<int> >::size_type i = 0; i != x.size() - 1; i++) {
pv(x[i]);
cout << endl << endl;
}
pv(x[x.size() - 1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment