Pagini recente » Cod sursa (job #320187) | Cod sursa (job #1211121)
#include <iostream>
using namespace std;
#include <fstream>
#include <math.h>
//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();
}