Cod sursa(job #2394312)

Utilizator S_AndyAndrei S S_Andy Data 1 aprilie 2019 15:36:00
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>
#include <math.h>

using namespace std;

ifstream fin("ciur.in");
ofstream fout("ciur.out");

int main()
{
	bool *v;
	int n, s = 0;
	fin >> n;
	v = new bool[n + 1];
	for (int i = 2; i <= n; ++i) {
		v[i] = 1;
	}
	int sqrt_n = sqrtf(n);
	for (int i = 2; i <= sqrt_n; ++i) {
		if (v[i]) {
			for (int j = i * 2; j <= n; j += i) {
				v[j] = false;
			}
		}
	}
	for (int i = 2; i <= n; ++i) {
		if (v[i]) {
			++s;
		}
	}
	fout << s;
}