Cod sursa(job #2531371)

Utilizator RobysenLazarov Robert Robysen Data 26 ianuarie 2020 10:49:16
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("ciur.in");
ofstream g("ciur.out");

const int nMax = 1000000 / 2 + 1;;

char v[nMax];

int main() {
	int n, prime = 1;
	f >> n;
	for (int i = 2; i <= n; i++)
		v[i] = 1;
	for (int i = 1; ((i * i) << 1) + (i << 1) <= n; i++) {
		if (v[i]) {
			for (int j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1)
				v[j] = 0;
		}
	}
	for (int i = 1; 2 * i + 1 <= n; i++)
		if (v[i]) prime++;
	g << prime;
}