Skip to content

Instantly share code, notes, and snippets.

@pddg
Last active April 27, 2018 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pddg/95ed423f824f22c084e25833ab7673d6 to your computer and use it in GitHub Desktop.
Save pddg/95ed423f824f22c084e25833ab7673d6 to your computer and use it in GitHub Desktop.
Leibniz in Go
package main
import (
"fmt"
"math"
)
func main() {
const MAX = 4
const N = 100000000
UNIT := int(N/MAX)
ch := make(chan float64, MAX)
for i := 0; i< MAX; i++ {
go func(begin int, end int) {
s := 0.0
for begin < end {
s += math.Pow(-1.0, float64(begin))/ float64(2 * begin + 1)
begin++
}
ch <- s
}(UNIT*i, UNIT*(i+1))
}
var s float64
for i := 0; i < MAX; i++ {
s += <- ch
}
fmt.Println(4 * s)
}
package main
import (
"fmt"
"math"
)
func main(){
n := 0
s := 0.0
nmax := int(math.Pow(10,8))
for n < nmax {
s += math.Pow(-1.0, float64(n))/ float64(2 * n + 1)
n += 1
}
fmt.Println(4 * s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment