Pagini recente » Cod sursa (job #1775944) | Cod sursa (job #1144110) | Istoria paginii utilizator/ubb_oprimabuzurile_2016 | Cod sursa (job #2969261) | Cod sursa (job #806579)
Cod sursa(job #806579)
#include <fstream>
#include <iostream>
using namespace std;
ifstream in("ciur.in");
ofstream out("ciur.out");
class Eratosthenes{
int n;
int *lowerPrimes; // un vector ce va tine numarul de nr prime mai mici decat n
int *sieve; // will basically act as a bitmap, a nr will be prime if its corresponding bit is 0
int bits[35]; // bit i will be 2 to the power of i
void set(int x){
int posInt=x/32;
int posBit=x%32;
sieve[posInt]|=bits[posBit];
}
bool isSet(int x){
int posInt=x/32;
int posBit=x%32;
return sieve[posInt]&bits[posBit];
}
public:
Eratosthenes(int value){
int i,j;
n=value;
lowerPrimes=new int[n+1];
lowerPrimes[0]=lowerPrimes[1]=0;
sieve=new int[(n>>5)+1];
for(i=0;i<=(n>>5);++i)
sieve[i]=0;
bits[0]=1;
for(i=1;i<32;++i)
bits[i]=bits[i-1]<<1;
for(i=2;i*i<=n;++i){
if(!isSet(i)){
lowerPrimes[i]=lowerPrimes[i-1]+1;
for(j=i*i;j<=n;j+=i){
set(j);
}
continue;
}
lowerPrimes[i]=lowerPrimes[i-1];
}
for(i=sqrt((float)n);i<=n;++i){
if(!isSet(i)){
lowerPrimes[i]=lowerPrimes[i-1]+1;
continue;
}
lowerPrimes[i]=lowerPrimes[i-1];
}
}
int countPrimes(int value){
return lowerPrimes[value];
}
};
int main(){
int n;
in>>n;
Eratosthenes ciur(n);
out<<ciur.countPrimes(n);
return 0;
}