Cod sursa(job #697340)

Utilizator gabrielvGabriel Vanca gabrielv Data 29 februarie 2012 02:23:47
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
using namespace std;
#include<cstdio>
#define MAX 2000005
char p[MAX];

int prim(int n) 
{
  int i, j, nr = 1;
  for (i = 1; ((i * i) << 1) + (i << 1) <= n; i += 1) {      
    if ((p[i >> 3] & (1 << (i & 7))) == 0) {
      for (j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1) {
        p[j >> 3] |= (1 << (j & 7));
      }
    }
  }
  for (i = 1; 2 * i + 1 <= n; ++i)  
       if ((p[i >> 3] & (1 << (i & 7))) == 0) 
           nr++;
  return nr;
}
int main()
{
	freopen("ciur.in","r",stdin);
	freopen("ciur.out","w",stdout);
	int n,c;
	scanf("%d",&n);
	c=prim(n);
	printf("%d",c);
}
/*using namespace std;
#include<cstdio>
#include<bitset>
#define MAX 2000005
bitset <MAX> ciur;
int n,i,j,c;
int main()
{
	freopen("ciur.in","r",stdin);
	freopen("ciur.out","w",stdout);
	scanf("%d",&n);
	for(i=3;i<=n;i=i+2)
		if(!ciur[i]) 
		{
			c++;
			for(j=i+i+i;j<=n;j=j+i+i)
				ciur[j]=1;
		}
	printf("%d",c+1);
	return 0;
}*/