JosepRivaille

P89124: Garbell d'Eratòstenes

Jun 2nd, 2015
1,870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. const int MAX = 1000000;
  7.  
  8. vector<bool> garbell(int n) {
  9.     vector<bool> aux(n+1, true);
  10.     for (int i = 2; i <= n; ++i) {
  11.         for (int j = 2*i; j <= n; j += i) {
  12.             aux[j] = false;
  13.         }      
  14.     }
  15.     return aux;
  16. }
  17.  
  18.  
  19. int main() {
  20.     int n;
  21.     vector<bool> auxiliar;
  22.     auxiliar = garbell(MAX);
  23.     while (cin >> n) {
  24.         if (n == 0 || n == 1) cout << n << " no es primer" << endl;
  25.         else {
  26.             if (auxiliar[n]) cout << n << " es primer" << endl;
  27.             else cout << n << " no es primer" << endl;
  28.         }
  29.     }
  30. }
  31.  
  32. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment