Pagini recente » Cod sursa (job #1451960) | Cod sursa (job #2427623) | Cod sursa (job #1261847) | Cod sursa (job #2930653) | Cod sursa (job #1766214)
#include <fstream>
#include <bitset>
#include <cmath>
using namespace std;
const int maxLength = 2000000 + 1;
int computeSieve(bitset<maxLength> &isNotPrime, int N)
{
int i, j, nonPrimes = 1;
isNotPrime[0] = isNotPrime[1] = 1;
for (i = 4; i <= N; i += 2)
{
isNotPrime[i] = 1;
nonPrimes++;
}
for (i = 3; i <= sqrt(N); i += 2)
if (!isNotPrime[i])
for (j = i * i; j <= N; j += i + i)
{
if (isNotPrime[j] == 0)
{
nonPrimes++;
isNotPrime[j] = 1;
}
}
return N - nonPrimes;
}
int main()
{
int N;
bitset<maxLength> isNotPrime;
ifstream f("ciur.in");
f >> N;
f.close();
ofstream g("ciur.out");
g << computeSieve(isNotPrime, N);
g.close();
return 0;
}