csansoon

P10.05 P89124 Sieve of Eratosthenes

Dec 23rd, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 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. int main() {
  19.         int n;
  20.         vector<bool> auxiliar;
  21.         auxiliar = garbell(MAX);
  22.         while (cin >> n) {
  23.                 if (n == 0 || n == 1) cout << n << " is not prime" << endl;
  24.                 else {
  25.                         if (auxiliar[n]) cout << n << " is prime" << endl;
  26.                         else cout << n << " is not prime" << endl;
  27.                 }
  28.         }
  29. }
  30.  
  31. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment