Cod sursa(job #1231770)

Utilizator thinkphpAdrian Statescu thinkphp Data 21 septembrie 2014 14:47:08
Problema Ciurul lui Eratosthenes Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.89 kb
#include <stdio.h>
#include <stdbool.h>
#define MAXN 2000001
#define FIN "ciur.in"
#define FOUT "ciur.out"

 
bool sita[ MAXN ];

int N, 
    count = 0;

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

int main() {

    read();
    eratosthenes(); 

    return(0);
};

void read() {

     freopen(FIN, "r", stdin);

     scanf("%d", &N);

     fclose( stdin );
}

void eratosthenes() {

     int i,
         j;

     freopen(FOUT, "w", stdout);

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

        if(sita[ i ]) {

           j = 2;

           while( i * j <= N) {

                  sita[ i * j ] = false;

                  j++; 
           }    
        }
     }

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

          if( sita[ j ] ) count++;
     }
  
  
     printf("%d", count);

     fclose( stdout );  
};