Cod sursa(job #3230077)

Utilizator mariaaxMaria Tomita mariaax Data 19 mai 2024 00:42:16
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
    int n, nr = 0;
    unsigned long long v[2000000];
    fin >> n;

    v[2] = 1;
    for(int i = 3; i <= n; i++) {
        if(i % 2 == 0){
            v[i] = 0;
        } else {
            v[i] = 1;
        }
    }

    for(int i = 3; i <= n; i += 2) {
        if(v[i] == 1) {
            for(int x = i * 2; x <= n; x += i) {
                v[x] = 0;
            }
        }
    }

    for(int i = 2; i <= n; i++){
        if(v[i] == 1) {
            nr++;
        }
    }

    fout << nr;

    return 0;
}