Write Program
Write
In C++, Sieve of Eratosthenes can be written as
cpp/src/prime.cpp
loading...
caution
extern "C"
is required here.
Build for Test
Build C++ program to executable file main.exe
.
g++
is used here.
build with optimization level -O3
g++ src/main.cpp src/prime.cpp -O3 -o main
g++ command line options can be found at 3 GCC Command Options.
Run and Test
Now, lets's run main.exe
and test the function.
invoke main function
main.exe 100000
This shows
output
given number = 100000
max prime = 99991
duration [ms] = 33
The following table shows the calculated prime for given "N".
given N | prime |
---|---|
2 | 2 |
3 | 3 |
10 | 7 |
100 | 97 |
1000 | 997 |
10000 | 9973 |
100000 | 99991 |