Cod sursa(job #2932518)

Utilizator coso2312Cosmin Bucur coso2312 Data 3 noiembrie 2022 08:13:03
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
using namespace std;


ifstream fin("ciur.in");
ofstream fout("ciur.out");

int main() {
    int n;
    fin >> n;
    int a[2000000];
    for (int i = 2; i <= n; ++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;
    return 0;
}