Created
September 3, 2020 00:30
-
-
Save n2p5/87fe2fb95583b886850f0399e3d8dfc7 to your computer and use it in GitHub Desktop.
calc all 10 digit primes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "math/big" | |
| "sync/atomic" | |
| ) | |
| func main() { | |
| start := 1000000001 | |
| end := 9999999999 | |
| checkin := 1000001 | |
| var counter uint64 | |
| pool := make(chan struct{}, 10000) | |
| for i := start; i <= end; i = i + 2 { | |
| if i%checkin == 0 { | |
| fmt.Println("computed:", i, "count at:", counter, "pool size: ", len(pool)) | |
| } | |
| pool <- struct{}{} | |
| go func(n int, done chan struct{}) { | |
| if big.NewInt(int64(n)).ProbablyPrime(0) { | |
| atomic.AddUint64(&counter, 1) | |
| } | |
| <-done | |
| }(i, pool) | |
| } | |
| fmt.Println("draining the pool") | |
| for { | |
| if len(pool) == 0 { | |
| close(pool) | |
| break | |
| } | |
| } | |
| fmt.Println("total number of primes:", counter) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment