Skip to content

Instantly share code, notes, and snippets.

@maksadbek
Created Mar 13, 2021
Embed
What would you like to do?
Calculate primes using Doug McIlroy's idea: http://swtch.com/~rsc/thread/
package main
import "sync"
func primes() {
var recur func(ch chan int)
wg := sync.WaitGroup{}
recur = func(ch chan int) {
p := <-ch
newch := make(chan int)
println(p)
wg.Add(1)
go recur(newch)
for n := range ch {
if n%p != 0 {
newch <- n
}
}
close(newch)
wg.Done()
}
ch := make(chan int)
go recur(ch)
for i := 3; i < 135; i++ {
ch <- i
}
close(ch)
wg.Wait()
}
func main() {
primes()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment