Advertisement
SMASIF

Sieve Algorithm

Aug 23rd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. int arr[100]={0};
  6.  
  7. int main()
  8. {
  9.     int num;
  10.     cin>>num;
  11.  
  12.     for(int i=2;i<sqrt(num);i++)
  13.     {
  14.         if(arr[i]==0)
  15.         {
  16.            for(int j=2;j*i<num;j++)
  17.            {
  18.                arr[i*j]=1;
  19.            }
  20.         }
  21.     }
  22.     for(int i=2;i<=num;i++)
  23.     {
  24.         if(arr[i]==0){
  25.             cout<<i<<" ";
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement