Cod sursa(job #280879)

Utilizator tomescu_alinTomescu Alin tomescu_alin Data 13 martie 2009 17:11:08
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
	unsigned int totalPrimes = 0;
	unsigned long n;

	cout << "Enter the limit: ";
	cin >> n;
	
	bool isPrime[n+1];
	totalPrimes = n - 1; //	(1 is not prime)
    
	for(int i = 0; i < n+1; i++) {
		isPrime[i] = true;
	}
    
	unsigned long i, j;
	for (i = 2; i*i <= n; i++) {
		if (isPrime[i]) {
			j = 2;
			while (i*j <= n) {
				unsigned long multiple = i*j;
				if(isPrime[multiple]) 
					totalPrimes--;
				isPrime[multiple] = 0;
				j++;				  
            }
        }
    }
	
    /*
	for (i = 2; i <= n; i++) {
		if (isPrime[i]) {
			cout << i << " ";
		}		
	}
	*/
	
	ofstream fout("ciur.out");
	fout << totalPrimes;
	//cout << endl << totalPrimes;
	fout.close();
	
	return 0;
}