Sunday, 13 October 2013

Sieve of Eratosthenes in c++

/*

Sieve of Eratosthenes

*/

#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
    int n,t;
    cin>>q;
    while(q--)
    {
    cin>>n;
    int ar[n];
    for(int i=2;i<n;++i)
            ar[i]=1;
    for(int i=2;i<n;++i)
    {
        if(ar[i])
            for(int j=i;j*i<n;++j)
                ar[i*j]=0;
    }
    cout<<"1"<<endl;
    for(int i=2;i<n;++i)
    {
        if(ar[i])
            cout<<i<<endl;
    }
    }
    return 0;
}

No comments:

Post a Comment