Skip to content

Instantly share code, notes, and snippets.

@shouth
Last active August 11, 2019 20:09
Show Gist options
  • Save shouth/d2c96b11030f4a88313b6882afb00662 to your computer and use it in GitHub Desktop.
Save shouth/d2c96b11030f4a88313b6882afb00662 to your computer and use it in GitHub Desktop.
#include<cmath>
#include<iomanip>
#include<iostream>
#include<gmpxx.h>
int main() {
unsigned digit = 10000;
mpf_set_default_prec((digit + 1) * log2(10));
mpf_class pi = 0;
unsigned m = digit / 4;
mpz_class p5 = 5;
for (unsigned n = 0; n < 3 * m + 2; n++) {
pi += (n % 2 == 0 ? 16_mpf : -16_mpf) / ((2 * n + 1) * p5);
p5 *= 25;
}
mpz_class p239 = 239;
for (unsigned n = 0; n < m; n++) {
pi += (n % 2 == 1 ? 4_mpf : -4_mpf) / ((2 * n + 1) * p239);
p239 *= 57121;
}
std::cout << std::fixed << std::setprecision(digit + 1) << pi << std::endl;
}
@shouth
Copy link
Author

shouth commented Jun 24, 2019

g++ calc_10000_digits_of_pi.cpp -o calc_10000_digits_of_pi -lmpfr -lgmp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment