-
12核心跑满计算质数
func main() { primes := []int{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,71,73,79,83,89,97} var limit = make(chan int , 12) m := sync.Mutex{} for i := 100; i < 10000000; i++ { limit <- i go func(i int) { p := true for j := 0;j < len(primes); j++ { pri := primes[j] if float64(pri) <= math.Sqrt(float64(i)) && i%pri == 0 { p = false break } } if p { fmt.Println(i) m.Lock() primes = append(primes, i) m.Unlock() } <- limit }(i) } }
有疑问加站长微信联系(非本文作者)