Cod sursa(job #2669177)

Utilizator thinkphpAdrian Statescu thinkphp Data 6 noiembrie 2020 12:37:54
Problema Ciurul lui Eratosthenes Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include <iostream>
#include <fstream>
#define FIN "ciur.in"
#define FOUT "ciur.out"

typedef unsigned long ulong;

namespace Math {

	namespace Eratosthenes {

              ulong getCount(ulong n) {

                    bool primes[ n + 1 ];

                    ulong totalPrimes = n - 1;

                    ulong i, j;

                    for (ulong i = 2; i < n + 1; ++i)
                    {
                       	 primes[ i ] = true;   
                    }   

                 
                    for (i = 2; (i * i) <= n; ++i)      	
                    {
                         if( primes[ i ] ) {

                         	 j = 2; 

                         	 while((i * j) <= n) {

                                   ulong multiply = i * j;

                                   if(primes[ multiply ]) totalPrimes--;

                                   primes[multiply] = false;

                                   j++;                         	 	
                         	 }
                         }
                    }

                return totalPrimes; 
              } 

	}
}

int main() {
     
	std::ifstream fin(FIN);
	std::ofstream fout(FOUT);
	ulong n;
    fin>>n;
    fout<<Math::Eratosthenes::getCount(n); 
    //std::cout<<Math::Eratosthenes::getCount(n); 
	return(0);
}