Cod sursa(job #1231764)

Utilizator thinkphpAdrian Statescu thinkphp Data 21 septembrie 2014 14:36:24
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#define MAXN 2000001
#define FIN "ciur.in"
#define FOUT "ciur.out"

using namespace std;
 
bool sita[ MAXN ];

int N, 
    count = 0;

//function prototypes
void eratosthenes();
void read();

int main() {

    read();
    eratosthenes(); 

    return(0);
};

void read() {

     ifstream f( FIN );

     f>>N; 

     f.close();
}

void eratosthenes() {

     int i,
         j;

     ofstream o( FOUT  );

     for(i = 2; i <= N; i++) sita[ i ] = true; 
 
     for(i = 2; i <= N; i++) {

        if(sita[ i ]) {

                 for( j = 2; (i * j) <= N; j++) {

                      sita[ i * j ] = false;
                 }
        }
     }

     for( j = 2; j <= N; j++) {

          if( sita[ j ] ) count++;
     }
  
     o<<count;

     o.close();
};