Cod sursa(job #472540)

Utilizator ilie.danilaIlie Teodor Danila ilie.danila Data 25 iulie 2010 15:42:42
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

char isNotPrim[1000001];
int n, i;

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

	f >> n;

	int maxCount  = n / 2;

	if( n % 2 == 0 ) maxCount--;

	f.close();

	int index = 3;
	int solution = 1;

	do
	{
		int multiply = index;
		while( index * multiply <= n)
		{
			isNotPrim[(index * multiply -1) / 2] = 1;
			multiply += 2;
		}
		if ( index * index < n )
			index += 2;
	}while( index * index < n );

	for( i = 1; i <= maxCount; i++ )
		if ( !isNotPrim[i] )
			solution++;

	g << solution << "\n";

	g.close();

	return 0;
}