Cod sursa(job #2922167)

Utilizator PingStrpewpewpac PingStr Data 5 septembrie 2022 15:41:25
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.66 kb
#include <fstream>

using namespace std;

int main()
{
    ifstream cin("ciur.in");
    ofstream cout("ciur.out");


    int n;
    cin>>n;
    if (n < 2 || n > 2000000)
        return 1;
    int A[n + 1];

    A[2] = 2;

    for (int i = 2, lim = n + 1; i < lim; i++)
    {
        for (int j = i + 1; j < lim; j++)
        {
            if (j % i == 0)
            {
                A[j] = -1;
            }
            else if (A[j] != -1)
            {
                A[j] = j;
            }
        }
    }

    for (int i = 2, lim = n + 1; i < lim; i++)
    {
        if (A[i] != -1)
            cout<<A[i]<<" ";
    }

    cin.close();
    cout.close();

    return 0;
}