Last active
January 18, 2016 20:44
-
-
Save andreuinyu/ddde025d8471b6bdcbc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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