Skip to content

Instantly share code, notes, and snippets.

@andreuinyu
Last active January 18, 2016 20:44
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 andreuinyu/ddde025d8471b6bdcbc3 to your computer and use it in GitHub Desktop.
Save andreuinyu/ddde025d8471b6bdcbc3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
int first_occurrence(int x, const vector<int>& v){
if (v.size()==0) return -1;
int inici = 0, final = v.size()-1;
while (inici <= final) {
int m = (inici + final)/2;
if (v[m] < x) inici = m + 1;
else final = m - 1;
}
if (final + 1 < v.size() and v[final + 1] == x){
return final + 1;
}
return -1;
}
vector<int> sedas(int n){
vector<int> B;
vector<bool> A(n+1, true);
for (int i = 2; i*i <= n; ++i){
if (A[i]){
B.push_back(i);
for (int j = i*i; j < n+1; j+=i){
A[j]=false;
}
}
}
return B;
}
int main(){
int a;
vector<int> u=sedas(1000001);
while (cin >> a){
if(first_occurrence(a, u) >= 0){
cout << a << " es primer" << endl;
}
else{
cout << a << " no es primer" << endl;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment