-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSieveofEratosthenes.C
More file actions
33 lines (25 loc) · 832 Bytes
/
SieveofEratosthenes.C
File metadata and controls
33 lines (25 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void SieveofEratosthenes(){
const int maxnum=1000000;
int bool_isprime[maxnum];
int counteroperations=0;
for (int m=0;m<maxnum;m++){
bool_isprime[m]=true;
counteroperations++;
}
for (int m=2;m<=int(sqrt(maxnum));m++){ // qui io luppo su i possibili multipli
for (int j=2;j<=int(maxnum/m);j++){ // e.g. se il multiplo e 3 allora il massimo numero di iterazioni sara int(1000/3)
if(m*j<maxnum) bool_isprime[m*j]=false;
counteroperations+=2;
}
}
TH1F*hPrimes=new TH1F("hPrimes","hPrimes",int(maxnum/1000),0,maxnum);
for (int m=2;m<maxnum;m++){
if (bool_isprime[m]==true) {
cout<<"il numero ="<<m<<"e primo"<<endl;
hPrimes->Fill(m);
}
counteroperations++;
}
cout<<"total number of operations="<<counteroperations<<endl;
hPrimes->Draw();
}