# 筛子算法之golang实现求素数解析

hellodyp · · 1183 次点击 · · 开始浏览

``````package main

import "fmt"

// Send the sequence 2, 3, 4, ... to channel 'ch'.
func generate(ch chan int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}

// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func filter(in, out chan int, prime int) {
for {
i := <-in // Receive value of new variable 'i' from 'in'.
if i%prime != 0 {
out <- i // Send 'i' to channel 'out'.
}
}
}

// The prime sieve: Daisy-chain filter processes together.
func main() {
ch := make(chan int) // Create a new channel.
go generate(ch)      // Start generate() as a goroutine.
for {
prime := <-ch
fmt.Print(prime, " \n")
ch1 := make(chan int)
go filter(ch, ch1, prime)
ch = ch1
}
}
``````

shaizi.png

generate这个函数很简单，用作产生无穷个从2依次递增的自然数

filter函数乍看也很简单，签名参数是两个通道和一个已知的素数，函数中是一个无限循环，每一轮循环中从输入通道`in`拿到待筛选的数据流，然后除以素数`prime`,不能整除的话则把被除数放入输出通道`out`，否则忽略掉被操作数，然后进入下一轮循环往复操作

``````// 第一轮  筛子2
3 5 7 9 11 13...
// 第二轮  筛子3
5 7 11 13...
//第三轮 筛子5
7 11 13 17 19...
``````

``````package main

import "fmt"

func generator() chan int{
ch := make(chan int)
go func() {
for i:=2; ;i++{
ch <- i
}
}()
return ch
}

func filter3(in chan int, primer int) chan int{
out := make(chan int)
go func() {
for {
i:= <- in
if i%primer != 0{
out <- i
}
}
}()
return out
}

func sieve2() chan int{
out := make(chan int)

go func() {
ch := generator()
for {
prime := <- ch
ch = filter3(ch, prime)
out <- prime
}
}()
return out
}

func main(){
primes := sieve2()
for{
fmt.Println(<-primes)
}

}
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

``````package main

import "fmt"

// Send the sequence 2, 3, 4, ... to channel 'ch'.
func generate(ch chan int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}

// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func filter(in, out chan int, prime int) {
for {
i := <-in // Receive value of new variable 'i' from 'in'.
if i%prime != 0 {
out <- i // Send 'i' to channel 'out'.
}
}
}

// The prime sieve: Daisy-chain filter processes together.
func main() {
ch := make(chan int) // Create a new channel.
go generate(ch)      // Start generate() as a goroutine.
for {
prime := <-ch
fmt.Print(prime, " \n")
ch1 := make(chan int)
go filter(ch, ch1, prime)
ch = ch1
}
}
``````

shaizi.png

generate这个函数很简单，用作产生无穷个从2依次递增的自然数

filter函数乍看也很简单，签名参数是两个通道和一个已知的素数，函数中是一个无限循环，每一轮循环中从输入通道`in`拿到待筛选的数据流，然后除以素数`prime`,不能整除的话则把被除数放入输出通道`out`，否则忽略掉被操作数，然后进入下一轮循环往复操作

``````// 第一轮  筛子2
3 5 7 9 11 13...
// 第二轮  筛子3
5 7 11 13...
//第三轮 筛子5
7 11 13 17 19...
``````

``````package main

import "fmt"

func generator() chan int{
ch := make(chan int)
go func() {
for i:=2; ;i++{
ch <- i
}
}()
return ch
}

func filter3(in chan int, primer int) chan int{
out := make(chan int)
go func() {
for {
i:= <- in
if i%primer != 0{
out <- i
}
}
}()
return out
}

func sieve2() chan int{
out := make(chan int)

go func() {
ch := generator()
for {
prime := <- ch
ch = filter3(ch, prime)
out <- prime
}
}()
return out
}

func main(){
primes := sieve2()
for{
fmt.Println(<-primes)
}

}
``````