Skip to main content

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 Nprime
22
33
107
10097
1000997
100009973
10000099991