Advertisement
ValerianBenchea

Ciurul lui Eratostene

Feb 25th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #define MAX 1000000
  4. using namespace std;
  5.  
  6. int v[MAX];//presupunem ... toate prime
  7.  
  8. int main()
  9. {
  10.     int i, n, j;
  11.     cout<<"n=";
  12.     cin>>n;
  13.     v[0]=v[1]=1;//NEPRIME
  14.     for(i=2; i<=sqrt(n); i++)
  15.     {
  16.         if(v[i]==0)//daca v[i] e prim
  17.         {
  18.             for(j=2; i*j<=n; j++)
  19.                 v[i*j]=1;//este neprim
  20.         }
  21.     }
  22.     for(i=1; i<=n; i++)
  23.         if(!v[i])
  24.             cout<<i<<" ";//toate numerele prime
  25.  
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement