artandlol
2018-09-25 15:45:05 +08:00
找素数的规律
package main
import (
"fmt"
"strconv"
)
func main() {
sieve()
}
func generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i
}
}
func filter(src <-chan int, dst chan<- int, prime int) {
str := ""
for i := 0; i < prime; i++ {
str += " "
}
for i := range src {
if i%prime != 0 {
dst <- i
}
}
}
func sieve() {
ch := make(chan int)
go generate(ch)
var olddouble,diff,doubleprime int
var count float64
for i := 0; i < 100; i++ {
prime := <-ch
doubleprime = prime*prime
diff = doubleprime - olddouble
strdiff := strconv.FormatInt(int64(diff), 10)
floatdiff,err := strconv.ParseFloat(strdiff, 64)
if err != nil {
fmt.Print("error!", "\n")
}
count = floatdiff / 24
olddouble = doubleprime
fmt.Print(prime,count, "\n")
ch1 := make(chan int)
go filter(ch, ch1, prime)
ch = ch1
}
}