Advertisement
Guest User

Untitled

a guest
Jan 4th, 2013
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <mpir.h>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7.     int digits;
  8.     int iterations;
  9.     int prec;
  10.     double lg;
  11.  
  12.     cout << "Digits: ";
  13.     cin >> digits;
  14.  
  15.     iterations = digits / 14;
  16.     iterations++;
  17.     lg = log(10) / log(2);
  18.     prec = (lg * digits) + 100;
  19.  
  20.     mpf_set_default_prec(prec);
  21.     mpf_t sigma;
  22.     mpf_t pi;
  23.     mpz_t topfact;
  24.     mpf_t minusone;
  25.     mpz_t bottomfact1;
  26.     mpz_t bottomfact2;
  27.     mpf_t top;
  28.     mpf_t bottom;
  29.     mpf_t temp;
  30.     mpf_t ftopfact;
  31.     mpf_t fbottomfact1;
  32.     mpf_t fbottomfact2;
  33.     mpf_t k2;
  34.     mpf_init(sigma);
  35.     mpf_init(pi);
  36.     mpz_init(topfact);
  37.     mpf_init(minusone);
  38.     mpz_init(bottomfact1);
  39.     mpz_init(bottomfact2);
  40.     mpf_init(top);
  41.     mpf_init(bottom);
  42.     mpf_init(temp);
  43.     mpf_init(ftopfact);
  44.     mpf_init(fbottomfact1);
  45.     mpf_init(fbottomfact2);
  46.     mpf_init(k2);
  47.     mpf_set_ui(k2, 545140134);
  48.     int k1 = 13591409;
  49.     int k3 = 640320;
  50.     int exp;
  51.     int k = 0;
  52.     int i;
  53.     int target;
  54.    
  55.     while(k <= iterations){
  56.         cerr << "[" << k << "|" << iterations << "]\r";
  57.         //-1^k
  58.         mpf_set_si(minusone, -1);
  59.         mpf_pow_ui(minusone, minusone, k);
  60.         if(k == 0){
  61.             mpf_set_ui(ftopfact, 1);
  62.             mpf_set_ui(fbottomfact1, 1);
  63.             mpf_set_ui(fbottomfact2, 1);
  64.         }
  65.         else if(k == 1){
  66.             mpf_set_ui(ftopfact, 720);
  67.             mpf_set_ui(fbottomfact1, 6);
  68.         }
  69.         else
  70.         {
  71.             target = 6 * k;
  72.             i = 6 * (k - 1);
  73.             i++;
  74.             for(;i <= target; i++){
  75.                 mpf_mul_ui(ftopfact, ftopfact, i);
  76.             }
  77.             target = 3 * k;
  78.             i = 3 * (k - 1);
  79.             i++;
  80.             for(;i <= target; i++){
  81.                 mpf_mul_ui(fbottomfact1, fbottomfact1, i);
  82.             }
  83.             mpf_mul_ui(fbottomfact2, fbottomfact2, k);
  84.         }
  85.         //(k1 + k2*k)
  86.         mpf_mul_ui(top, k2, k);
  87.         mpf_add_ui(top, top, k1);
  88.         //top = minusone * topfact * top
  89.         mpf_mul(top, top, minusone);
  90.         mpf_mul(top, top, ftopfact);
  91.  
  92.         //calculate exponent
  93.         exp = 3*k;
  94.         //k3 ^ exp
  95.         mpf_set_ui(bottom, k3);
  96.         mpf_pow_ui(bottom, bottom, exp);
  97.  
  98.         //bottom = bottomfact1 * bottomfact2 * bottom
  99.         mpf_mul(bottom, bottom, fbottomfact1);
  100.         mpf_mul(bottom, bottom, fbottomfact2);
  101.        
  102.         //sigma += top/bottom
  103.         mpf_div(temp, top, bottom);
  104.         mpf_add(sigma, sigma, temp);
  105.         k++;
  106.     }
  107.  
  108.     mpf_sqrt_ui(temp, 10005);
  109.     mpf_mul_ui(temp, temp, 426880);
  110.     mpf_ui_div(temp, 1, temp);
  111.     mpf_mul(pi, sigma, temp);
  112.     mpf_ui_div(pi, 1, pi);
  113.  
  114.     gmp_printf("%.Ff", pi);
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement