Cod sursa(job #161237)

Utilizator luana_0105Fagarasan Luana luana_0105 Data 17 martie 2008 19:37:47
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include<stdio.h>
#include<math.h>

#define nmax 2000001

int n, ct;
unsigned char t[nmax];

void read()
{
     freopen("ciur.in","r",stdin);
     freopen("ciur.out","w",stdout);
     scanf("%d", &n);
}

void ciur_erathostenes()
{
      int i;
      int radical= int (sqrt(n));
      for(i=2; i<=radical;++i)
         if(t[i]==0)
         {
             int k=i*i;
             while(k<=n)
             {
                 t[k]=1;
                 k=k+i;
             }
         }
}

void print()
{
       int i;
       for(i=2;i<=n;++i)
          if(t[i]==0)
             ct++;
       printf("%d\n",ct);
}


int main()
{
     read();
     ciur_erathostenes();
     print();
     return 0;
}