Cod sursa(job #1013463)

Utilizator bghimisFMI Ghimis Bogdan bghimis Data 20 octombrie 2013 23:18:29
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
// Stive of E.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <bitset>
#include <cstdio>
using namespace std;

void computePrimes( bitset<2000000> arrayOfPrimes, int supperiorBound)
{
	int i, j;

	for ( i = 1; i <= supperiorBound; ++i )
		arrayOfPrimes[i] = 1;

	for ( i = 2; i * i < supperiorBound; ++i )
	{
		if (arrayOfPrimes[i] == 0)
		{
			for ( j = i * i; j <= supperiorBound; j += i)
				arrayOfPrimes[j] = 1;
		}

	}
}

int main(int argc, char* argv[])
{
	freopen("ciur.in", "r", stdin);
	freopen("ciur.out", "w", stdout);

	bitset<2000000> arrayOfPrimes;
	int supperiorBound;		cin >> supperiorBound;

	computePrimes( arrayOfPrimes, supperiorBound );

	long k = 0;
	for (int i = 2; i <= supperiorBound; ++i)
		if ( arrayOfPrimes[i] == true )
			++k;
	cout << k;

	return 0;
}