Cod sursa(job #3204463)

Utilizator tudorhorotanHorotan Tudor tudorhorotan Data 16 februarie 2024 19:15:35
Problema Ciurul lui Eratosthenes Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("ciur.in");
ofstream fout("ciur.out");
int v[2000001];

void ciur (int n) {
    for (int i = 3; i <= n; i += 2) {
        if (v[i / 2 + 1] == 0){
            int j = 2;
            while (i * j <= n) {
                if (i * j % 2 == 1) {
                    v[(i * j) / 2 + 1] = 1;
                }
                ++j;
            }
        }
    }
}


int main() {
    int n;
    fin >> n;
    ciur(n);
    int s = 0;
    for(int i = 1; i <= n / 2 + 1; ++i) {
        if (v[i] == 0) {
            ++s;
        }
    }
    if (n == 2) {
        fout << 1;
    } else {
        fout << s - 1;
    }
    return 0;
}