mirror of
https://github.com/golang/go
synced 2024-11-05 20:46:10 -07:00
4391aca850
The old link died; replace with an archive.org copy. Fixes #13345. Change-Id: Ic4a7fdcf258e1ff3b4a02ecb4f237ae7db2686c7 Reviewed-on: https://go-review.googlesource.com/18335 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
35 lines
598 B
Go
35 lines
598 B
Go
// Concurrent computation of pi.
|
|
// See https://goo.gl/la6Kli.
|
|
//
|
|
// This demonstrates Go's ability to handle
|
|
// large numbers of concurrent processes.
|
|
// It is an unreasonable way to calculate pi.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println(pi(5000))
|
|
}
|
|
|
|
// pi launches n goroutines to compute an
|
|
// approximation of pi.
|
|
func pi(n int) float64 {
|
|
ch := make(chan float64)
|
|
for k := 0; k <= n; k++ {
|
|
go term(ch, float64(k))
|
|
}
|
|
f := 0.0
|
|
for k := 0; k <= n; k++ {
|
|
f += <-ch
|
|
}
|
|
return f
|
|
}
|
|
|
|
func term(ch chan float64, k float64) {
|
|
ch <- 4 * math.Pow(-1, k) / (2*k + 1)
|
|
}
|