Cod sursa(job #1211120)

Utilizator dm1sevenDan Marius dm1seven Data 21 iulie 2014 23:33:01
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
using namespace std;
#include <fstream>

//int e_010_eratosthenes
int eratosthenes(int N, char* v)
{
	for (int i = 1; i <= N; i++) v[i] = 0;
	int sn = (int) sqrt(N);
	int primes = 0; // 1 is prime
	for (int i = 2; i <= sn; i++) {
		if (v[i] == 0) {
			primes++;
			//filter out the multiples of i
			int mi = 2 * i;
			while (mi <= N) { v[mi] = 1; mi += i; }
		}
	}

	//count the rest of the primes
	for (int i = sn + 1; i <= N; i++) if (v[i] == 0) primes++;

	return primes;
}

int main()
{
	int N;
	const int MAX_N = 2000000;
	char v[MAX_N + 1];

	ifstream ifs("ciur.in");
	ifs >> N;
	ifs.close();

	int primes = eratosthenes(N, v);

	ofstream ofs("ciur.out");
	ofs << primes;
	ofs.close();
}