Cod sursa(job #1316945)

Utilizator ArambasaVlad Arambasa Arambasa Data 14 ianuarie 2015 12:51:24
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <cmath>
#define NMax 2000000
using namespace std;
ifstream in ("ciur.in");
ofstream out ("ciur.out");
bool eratostene[NMax];int n;
void Read ()
{
    in>>n;
}
bool prim (int a)
{
    bool sw=true;int d=2;
    while (sw&&d<=sqrt(a))
    {
        if (a%d==0)
        {
            sw=false;
        }
        else sw=true;
    }
    return sw;
}
void Solve ()
{
    int contor=0;
    for (int i=2;i<n;i++)
    {
        if (prim (i))
        {
            eratostene[i]=true;
            for (int j=i*i;j<n;j+=i)
            {

                eratostene[j]=false;
            }
        }
    }
    for (int i=0;i<n;i++)
    {
        if (eratostene[i]==true)
            contor++;
    }
    out<<contor;
}

int main()
{
    Read ();
    Solve ();
    return 0;
}