Created
January 27, 2024 17:24
-
-
Save sudipidus/2053387d9321c62b605ac00e3e6b4eec to your computer and use it in GitHub Desktop.
parallel prime number sieve in golang
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
// Communication Sequential Process (CSP) - Tony Hoare | |
// Finding Prime numbers using sieve in parallel | |
package main | |
import "fmt" | |
func main() { | |
src := make(chan int) | |
go Generate(src) | |
for i := 0; i < 100; i++ { | |
prime := <-src | |
fmt.Println(prime) | |
dest := make(chan int) | |
go Filter(src, dest, prime) | |
src = dest | |
} | |
} | |
func Generate(ch chan<- int) { | |
for i := 2; ; i++ { | |
ch <- i | |
} | |
} | |
func Filter(src <-chan int, dest chan<- int, prime int) { | |
for i := range src { // loop over the values received in source channel | |
if i%prime != 0 { | |
dest <- i //send to destination channel | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment