Cod sursa(job #1455327)

Utilizator petru.cehanCehan Petru petru.cehan Data 27 iunie 2015 16:16:10
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin ("ciur.in") ;
ofstream fout ("ciur.out") ;

int n ;
char a[2000001] ;

void Citire()
{
    fin >> n;
}
int nr = 0 ;

void CiurEratostene (int n)
{
    int i , j ;
    a[0] = '0';
    a[1] = '0';
    a[2] = '1';
    for ( i = 4 ; i <= n ; i += 2 )
            a[i] = '0';

    for ( i = 3 ; i <= n ; i += 2 )
            a[i] = '1';

    for( i = 3 ; i <= n ; i += 2 )
        if ( a[i] == 1 )
           for ( j = i << 2 ; j <= n ; j += i )
                   a[j]='0';
    a[n+1] = 0;

    cout << 2 << " ";
    for ( i=2 ; i <= n ; i += 2 )
       if ( a[i] == '1' )
           cout << i << " " , ++ nr ;
    fout<<nr ;
}
int main()
{
    Citire();
    CiurEratostene(n);
    return 0;
}