-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a working arbitrary integer solution to pi series
- Loading branch information
1 parent
408e513
commit 16e308e
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package elementary | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
// Write a program that computes the sum of an alternating series | ||
// where each element of the series is an expression of the form | ||
// ((-1)^{k+1})/(2 * k-1) | ||
// for each value of k from 1 to a million, multiplied by 4. | ||
|
||
// AlternatingSeries computes the series in exercise 11 and prints the reuslt | ||
func AlternatingSeriesBig() *big.Float { | ||
i := big.NewInt(1) | ||
limit := big.NewInt(1000000) | ||
sum := big.NewFloat(0) | ||
for i.Cmp(limit) == -1 { | ||
k := *i | ||
k.Add(&k, big.NewInt(1)) | ||
numerator := big.NewInt(0) | ||
numerator.Exp(big.NewInt(-1), &k, nil) | ||
|
||
k.Sub(&k, big.NewInt(1)) // k = k - 1 because 1 was added above and it's reused | ||
|
||
denominator := big.NewInt(0) | ||
denominator.Mul(big.NewInt(2), &k) | ||
denominator.Sub(denominator, big.NewInt(1)) | ||
|
||
value := big.NewFloat(0) | ||
value.SetPrec(100000) | ||
value.SetInt(numerator) | ||
value.Quo(value, big.NewFloat(0).SetInt(denominator)) | ||
|
||
sum.Add(sum, value) | ||
// fmt.Println("sum, value: ", sum, value) | ||
i.Add(i, big.NewInt(1)) | ||
} | ||
return sum.Mul(sum, big.NewFloat(4)) | ||
} |