Cod sursa(job #2697286)

Utilizator ivanciuc.dragos22Ivanciuc Dragos Vasile ivanciuc.dragos22 Data 18 ianuarie 2021 08:37:52
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

using namespace std;

ifstream f("ciur.in");
ofstream g("ciur.out");

bool p[2000001];
//void ciur(int x)
//{
//    long long k = x;
//    c[1] = c[0] = 1;
//    for(int i = 2 ; i * i <= x ; ++i)
//        for(int j = 2 ; i * j <= x ; ++j) {
//            c[i * j] = 1;
//        }
//
//    g << k;
//}
void getTheNumber(int n) {
    int i, j, nr = 0;
    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++;
    g << nr;
  }
int n;
int main()
{
    f >> n;
    getTheNumber(n);
    return 0;
}