Last active
April 27, 2018 03:48
-
-
Save pddg/95ed423f824f22c084e25833ab7673d6 to your computer and use it in GitHub Desktop.
Leibniz in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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