blob: 258cec2e1bff53566d0015c9837a15e69d9e02bb [file] [log] [blame]
// Concurrent computation of pi.
// The implementation uses the Nilakantha Series.
//
// 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(" math.Pi:", math.Pi)
fmt.Println("Nilakantha Series:", 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 := 3.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 + 2) * (2*k + 3) * (2*k + 4))
}