Cod sursa(job #1457508)

Utilizator tony.hegyesAntonius Cezar Hegyes tony.hegyes Data 3 iulie 2015 15:41:43
Problema Ciurul lui Eratosthenes Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>
using namespace std;

int main(int argc, char **argv)
{
	// INPUT
	int n;
	ifstream indata("ciur.in");
	indata >> n;	
	indata.close();

	// PRIME NUMBER IDENTIFICATION
	n += (n % 2 == 0) ? -1 : 0;
	int m = (n >= 1) ? 1 : 0;
	int ciur[(n / 2) + 1];
	for (int i = 0; i <= (n / 2); i++) {
		ciur[i] = 1;
	}
	
	for (int i = 1; i <= (n / 2); i++) {
		if (ciur[i] == 1) {
			m++; 
			for (int j = i; j <= (n / 2); j += (i * 2 + 1)) {
				ciur[j] = 0;
			}
		}
	}
	
	// OUTPUT
	ofstream outdata("ciur.out");
	outdata << m;
	outdata.close();
	
	return 0;
}