-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsieve-v-11.cpp
49 lines (45 loc) · 1.19 KB
/
sieve-v-11.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
int eratosthenes(int lastNumber)
{
char *isPrime = new char[lastNumber + 1];
for (int i = 0; i <= lastNumber; i++)
isPrime[i] = 1;
for (int i = 2; i * i <= lastNumber; i++)
if (isPrime[i])
for (int j = i * i; j <= lastNumber; j += i)
isPrime[j] = 0;
int found = 0;
for (int i = 2; i <= lastNumber; i++)
found += isPrime[i];
delete[] isPrime;
return found;
}
int main(int argc, char *argv[])
{
int n = 100;
if (argc > 1)
{
try
{
n = stoi(argv[1]);
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
}
}
auto start = high_resolution_clock::now();
// SieveOfEratosthenes(n);
eratosthenes(n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
// cout << n << " prime numbers in\n";
// cout << duration.count() << " microseconds" << endl;
// cerr << duration.count() << endl;
cout << duration.count();
return 0;
}
// g++ -std=c++11 basic.cpp && ./a.out