Cod sursa(job #1251518)

Utilizator Alexa2001Alexa Tudose Alexa2001 Data 29 octombrie 2014 17:14:20
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include <cstdio>


using namespace std;
int i,a[2000009],nr,n,j;
int main()
{
    freopen("ciur.in","r",stdin);
    freopen("ciur.out","w",stdout);

    scanf("%d",&n);


  for (i = 1; ((i * i) << 1) + (i << 1) <= n; i += 1)
    {
        if ((a[i >> 3] & (1 << (i & 7))) == 0)
        {
            for(j = ((i * i) << 1) + (i << 1); (j << 1) + 1 <= n; j += (i << 1) + 1)
            {
                a[j>>3] |= (1<<(j & 7));
            }
        }
    }
    nr=0;
    for(i=1; ((i<<1)+1)<=n; ++i)
        if((a[i>>3] & (1<<(i&7)))==0) ++nr;
/*

  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++;
 */
    printf("%d\n",nr+1);

    return 0;
}