Cod sursa(job #1013468)

Utilizator bghimisFMI Ghimis Bogdan bghimis Data 20 octombrie 2013 23:26:46
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
// Stive of E.cpp : Defines the entry point for the console application.
//
//#include "StdAfx.h"

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

bitset<2000005> arrayOfPrimes;

void computePrimes(int supperiorBound)
{
	int i, j;

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

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

	}
}

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

	int supperiorBound;		cin >> supperiorBound;

	computePrimes( supperiorBound );

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

	return 0;
}