Skip to content

Instantly share code, notes, and snippets.

@n2p5
Created September 3, 2020 00:30
Show Gist options
  • Select an option

  • Save n2p5/87fe2fb95583b886850f0399e3d8dfc7 to your computer and use it in GitHub Desktop.

Select an option

Save n2p5/87fe2fb95583b886850f0399e3d8dfc7 to your computer and use it in GitHub Desktop.
calc all 10 digit primes
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