Advertisement
Dani_info

Ciurul lui Eratostene

Apr 12th, 2018
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.     int n;
  9.     cout << "n="; cin >> n;
  10.     int v[100], k = 0, ind = 0;
  11.     int c[100];
  12.     c[ind++] = 2;
  13.     for (int i = 3; i <= n; i += 2)
  14.         v[k++] = i;
  15.     for (int i = 0; i<k; i++)
  16.     {
  17.         if (v[i] != 0)
  18.             c[ind++] = v[i];
  19.         for (int j = i + 1; j<k; j++)
  20.         {  
  21.             if (v[i] != 0)
  22.             {
  23.                 if (v[j] % v[i] == 0)
  24.                     v[j] = 0;
  25.             }
  26.         }
  27.     }
  28.     for (int i = 0; i<ind; i++)
  29.         cout << c[i] << " ";
  30.     system("pause");
  31.     return 0;
  32. }
Advertisement
RAW Paste Data Copied
Advertisement