mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-05-09 11:42:57 +00:00

Former-commit-id: bdede6ed1b6f578f2ef046c338caf02d0b29d453 [formerly 7187de361b53e9c8ec121df379b762f2db736ea2] Former-commit-id: 447d58460fbbfd05ffe08428a1288e392637561d
35 lines
641 B
Go
35 lines
641 B
Go
// Concurrent computation of pi.
|
|
// See http://goo.gl/ZuTZM.
|
|
//
|
|
// 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)
|
|
}
|