Cod sursa(job #146461)

Utilizator vlad_olteanVladimir Oltean vlad_oltean Data 1 martie 2008 19:03:24
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include<fstream>
using namespace std;

int main()
{
    unsigned n;
    bool prim[2000005];
    int count=0;
    unsigned sol[1009];
    unsigned i,j;

    ifstream fin("ciur.in");
    ofstream fout("ciur.out");
    fin>>n;
    
    prim[0]=0; prim[1]=0;
    for(i=2;i<=n;i++) prim[i]=1;
    for(i=4;i<=n;i+=2) prim[i]=0;    

    for(i=3;i<=n;i+=2)
         if(prim[i])
              for(j=i*i;j<=n;j+=i) prim[j]=0; // Elimin toti multiplii

    for(i=(n%2)?n:n-1;i>=3;i-=2)
         if(prim[i])
         {    if(count==1000) break;
              sol[count]=i;
              count++;
         }

    sol[count]=2; count++;

    fout<<count<<endl;
    for(i=count-1;i;i--)
	     fout<<sol[i]<<' ';
    fout<<endl;
    fin.close();
    fout.close();
    return 0;
}