This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpi.go
69 lines (59 loc) · 1.74 KB
/
pi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Find π to the Nth digit
// Since float64 can only hold ~16 decimal digits, we're going to need to use
// Go's math/big package for arbitrary precision arithmetic.
package main
import (
"fmt"
"math/big"
)
func main() {
var n int
fmt.Printf("Compute π to how many decimal places? (≤200) ")
_, err := fmt.Scanf("%d", &n)
if err != nil {
fmt.Println("Error:", err)
return
}
if n > 200 {
fmt.Println("Error:", n, "> 200 - too many decimal places!")
return
} else if n < 0 {
fmt.Println("Error:", n, "< 0")
return
}
/* Use Simon Plouffe's decimal digit extraction formula to calculate π.
For more information, see the following article:
Plouffe, Simon. "On the computation of the nth decimal digit of various
transcendental numbers." (1996) http://arxiv.org/pdf/0912.0303.pdf
The general formula used is shown below:
∞ n*2^n*(n!)^2
π + 3 = Σ ------------
n=1 (2*n)!
*/
pi := big.NewRat(0, 1)
tempInt := big.NewInt(1)
term := big.NewRat(0.0, 1.0)
for k := 1; k < 4*n+10; k++ {
top := big.NewInt(int64(k))
tempInt = tempInt.Exp(big.NewInt(2), big.NewInt(int64(k)), nil)
top = top.Mul(top, tempInt)
tempInt = tempInt.Exp(tempInt.Set(fact(k)), big.NewInt(2), nil)
top = top.Mul(top, tempInt)
bottom := fact(2 * k)
term := term.SetFrac(top, bottom)
pi.Add(pi, term)
}
pi = pi.Add(pi, big.NewRat(-3, 1))
fmt.Println("π to", n, "decimal places: ")
fmt.Println(pi.FloatString(n))
}
// Simple factorial helper function.
// Takes an int but returns a *big.Int since factorial results are much much
// larger than their arguments.
func fact(a int) *big.Int {
b := big.NewInt(1)
for i := a; i > 1; i-- {
b = b.Mul(b, big.NewInt(int64(i)))
}
return b
}