Cod sursa(job #3204444)

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

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;
        }
    }
    fout << s;
    return 0;
}