You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lavos/cap3/ex1.go

18 lines
356 B

package cap3
// Ex1 例题1
// 求1/1! - 1/3! + 1/5! - 1/7! + ... + (-1)^(n+1)/(2n-1)!
func Ex1(n int) float64 {
sum, fab := 0.0, uint64(1)
for i := 1; i <= n; i++ {
fab *= 2*uint64(i) + 1
if i%2 == 0 {
sum += -1.0 / float64(fab)
} else {
sum += 1.0 / float64(fab)
}
}
return sum
}