Cod sursa(job #777656)

Utilizator icb_mnStf Cic icb_mn Data 12 august 2012 22:48:14
Problema Ciurul lui Eratosthenes Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include<fstream>
#include<malloc.h>
using namespace std;

ifstream f("ciur.in");
ofstream g("ciur.out");

#define NMAX 2000000

int n = 0,nr = 0;
short a[NMAX];

void eratosthenes()
{
	for(int i = 4; i <= n; i+=2)
		a[i] = 1;
	
	for(int i = 3; i <= n; i += 2)
	{
		if(!a[i])
		{
			for(int j = i + i + i; j <= n; j += i + i )
				a[j] = 1;
		}
	}
}

int main()
{
	f>>n;
	
	eratosthenes();
	
	for(int i = 2; i <= n; ++i)
		if(a[i] == 0)
			nr++;
	
	g<<nr;
	
	g.close();
	
	return 0;
}