Cod sursa(job #2560389)

Utilizator sky_walkerRusti Paula sky_walker Data 27 februarie 2020 22:23:09
Problema Ciurul lui Eratosthenes Scor 50
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <stdio.h>
#include <stdlib.h>
int arr[2000005]; //global to be initialized with 0
int main()
{
    int n, cnt=0;
    FILE *fin=fopen("ciur.in", "r");
    FILE *fout=fopen("ciur.out", "w");
    fscanf(fin, "%d", &n);

    for(int i=2; i<=n; i++) //the indexes are all numbers up to n and the array values mean that they are prime or not
        arr[i]=1;   //mark all the numbers as prime

    for (int i=2; i<=n; i++)
    {
        if (arr[i]) //at the first iteration it will consider 2 which is prime and will be marked as 1
        {
            cnt++; //count the primes
            for (int j=i+i; j<=n; j+=i)
            {
                arr[j]=0; //unmark the multiples of the prime number i
            }
        }
    }

    fprintf(fout, "%d", cnt);

    return 0;
}