Write Program
Write
In Go, Sieve of Eratosthenes can be written as
go/src/prime.go
loading...
Build for Test
Build Go program to executable file.
This command generates main.exe
.
build with go
go build -tags=release -ldflags="-s -w" src/main.go src/prime.go
Here, -tags=release
and -ldflags
are for optimizing.
Go command line options can be found at here.
Run and Test
Now, run the executable file.
And, invoke and test the function.
invoke main function
main.exe 100000
output
given number = 100000
max prime = 99991
duration [ns] = 63042700
Or, go
command can directly invoke the program and results in same output.
directly invoke main function
go run -tags=release -ldflags="-s -w" src/main.go src/prime.go 100000
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 |