_content/doc/play: use the Nilakantha Series to compute pi
The Nilakantha Series converge more quickly comparing to the
Gregory-Leibniz Series.
Fixes golang/go#53598.
Change-Id: I9f2bbdde403b302d652171a76dddc623d2291712
GitHub-Last-Rev: 81327fe84c36f0befcc27c95c220b7f24670e811
GitHub-Pull-Request: golang/website#165
Reviewed-on: https://go-review.googlesource.com/c/website/+/414826
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Galvin Gao <galvingaoper@gmail.com>
diff --git a/_content/doc/play/pi.go b/_content/doc/play/pi.go
index f3c3f35..258cec2 100644
--- a/_content/doc/play/pi.go
+++ b/_content/doc/play/pi.go
@@ -1,5 +1,5 @@
// Concurrent computation of pi.
-// See https://goo.gl/la6Kli.
+// The implementation uses the Nilakantha Series.
//
// This demonstrates Go's ability to handle
// large numbers of concurrent processes.
@@ -12,7 +12,8 @@
)
func main() {
- fmt.Println(pi(5000))
+ fmt.Println(" math.Pi:", math.Pi)
+ fmt.Println("Nilakantha Series:", pi(5000))
}
// pi launches n goroutines to compute an
@@ -22,7 +23,7 @@
for k := 0; k < n; k++ {
go term(ch, float64(k))
}
- f := 0.0
+ f := 3.0
for k := 0; k < n; k++ {
f += <-ch
}
@@ -30,5 +31,5 @@
}
func term(ch chan float64, k float64) {
- ch <- 4 * math.Pow(-1, k) / (2*k + 1)
+ ch <- 4 * math.Pow(-1, k) / ((2*k + 2) * (2*k + 3) * (2*k + 4))
}