Cod sursa(job #2766659)

Utilizator AdvisorySamachis Andrei Advisory Data 2 august 2021 18:02:11
Problema Ciurul lui Eratosthenes Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <fstream>
using namespace std;

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

bool v[2000001];

int main() {
    int n, i, j, raspuns = 1;
    fin >> n;
    if(n < 2)
        fout << 0;
    else {
        v[1] = true; // daca e prim => false
        v[0] = true;
        for(j = 2 * 2; j <= n; j += 2)
            v[j] = true;
        for(i = 3; i <= n; i += 2) {
            if(!v[i]) {
                ++raspuns;
                for(j = i * i; j <= n; j += i) {
                    v[j] = true;
                }
            }
        }
    }
    fout << raspuns;
    return 0;
}
/*

 n = 10;
 out = 4?

2: 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32
3: 6, 9, 12, 15, 18, 21, 24, 27, 30
5: 10, 15, 20, 25, 30, 35
7: 14. 21, 28, 35, 42, 49
 */