Cod sursa(job #2926246)

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

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

int a[2000000];

int main() {

    int n;
    fin >> n;
    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;
}