Advertisement
Funnnny

Overcomplicated Pi

Jul 28th, 2013
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.72 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "math"
  6.     "math/big"
  7. )
  8.  
  9. func atanStep(n int, sign int64, atan chan big.Rat) {
  10.     y := float64(2*n + 1)
  11.     a := big.NewRat(4, int64(math.Pow(5, y)))
  12.     a.Sub(a, big.NewRat(1, int64(math.Pow(239, y))))
  13.     a.Mul(a, big.NewRat(4*sign, int64(y)))
  14.     atan <- *a
  15. }
  16.  
  17. func Pi(prec int) string {
  18.     result := big.NewRat(0, 1)
  19.     step := int(float64(prec)/1.4 + 1)
  20.     var sign int64 = 1
  21.     atan := make(chan big.Rat)
  22.     for i := 0; i <= step; i++ {
  23.         go atanStep(i, sign, atan)
  24.         sign *= -1
  25.     }
  26.     for i := 0; i <= step; i++ {
  27.         a := <-atan
  28.         result.Add(result, &a)
  29.     }
  30.     return result.FloatString(prec)
  31. }
  32.  
  33. func main() {
  34.     var n int
  35.     fmt.Printf("Precision:")
  36.     fmt.Scanf("%d", &n)
  37.     fmt.Println("Pi: ", Pi(n))
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement