Pagini recente » Cod sursa (job #2575838) | Cod sursa (job #2967176) | Cod sursa (job #1382981) | Cod sursa (job #934285) | Cod sursa (job #1206435)
//
// main.cpp
// OOP - Ciurul lui Eratosthenes
//
// Created by Alex Petrache on 09/07/14.
// Copyright (c) 2014 Alex Petrache. All rights reserved.
//
#include <iostream>
#include <fstream>
#define N_MAX 3000000
using namespace std;
class Eratosthenes{
int n;
char a[N_MAX];
int count;
public:
Eratosthenes(int n);
int getNumberOfPrimes();
int* getPrimes(int arr[]);
};
Eratosthenes::Eratosthenes(int x){
n=x;
count=0;
int i,j;
for(i=2;i<=n;i++){
if(a[i]==0){
count++;
for(j=i+i;j<=n;j+=i)
a[j]=1;
}
}
}
int Eratosthenes::getNumberOfPrimes(){
return count;
}
int* Eratosthenes::getPrimes(int arr[]){
int i,j=0;
for(i=2;i<=n;i++)
if(a[i]==0){
arr[j++]=i;
}
return arr;
}
int main(int argc, const char * argv[])
{
int n;
ifstream f("ciur.in");
ofstream g("ciur.out");
cin>>n;
Eratosthenes interval(n);
cout<<interval.getNumberOfPrimes();
//cout<<'\n';
/*int array[20000];
int *vector=interval.getPrimes(array);
int i=0;
while(vector[i]){
cout<<vector[i]<<" ";
i++;
}*/
return 0;
}