Cod sursa(job #2926245)

Utilizator coso2312Cosmin Bucur coso2312 Data 17 octombrie 2022 14:03:24
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream fin("ciur.in");
    ofstream fout("ciur.out");
    int a[2000000], n;
    fin >> n;
    for (int i = 2; i <= 2000000; ++i) {
        a[i] = 0;
    }
    for (int i = 2; i * i <= n; ++i) {
        int ok = 0;
        if (i % 2 == 0 && i != 2 || i % 3 == 0 && i != 3) {
            ++ok;
        }
        if (ok == 0) {
            for (int j = i; j * i <= n; ++j) {
                a[i * j] = 1;
            }
        }
    }
    int counter = 0;
    for (int i = 2; i <= n; ++i) {
        if (a[i] == 0) {
            ++counter;
        }
    }
    fout << counter;
}