jukaukor

BorweinPlouffePi_nth.cpp

Jul 31st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. /***************************************************************
  2. * Computing nth hex digit of Pi with BBP formula.
  3. * Bailey-Borwein-Plouffe_formula for Pi
  4. * (original https://gist.github.com/komasaru/9e418eb666ab649ef589)
  5. * removed class declarations, uses only functions
  6. * Juhani Kaukoranta 31.7.2019
  7. **************************************************************/
  8. #include <math.h>
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. using namespace std;
  14.  
  15. // Declaration
  16. int n; // nth Digit to compute
  17. double pi; // Pi
  18. char pi_hex[14]; // Pi(Hex)
  19. clock_t t0, t1; // Time
  20.  
  21.  
  22. /*
  23. * Convert Pi to Hex-strings
  24. */
  25. void convHex(double x, char chx[]) {
  26. double y;
  27. int i;
  28. const char hx[] = "0123456789ABCDEF";
  29.  
  30. y = fabs(x);
  31. for (i = 0; i < 16; i++) {
  32. y = 16.0 * (y - floor(y));
  33. chx[i] = hx[(int)(y)];
  34. }
  35. }
  36.  
  37. /*
  38. * Compute Modular Exponentiation
  39. */
  40. long compModExp(int b, int e, int m)
  41. {
  42. long ans;
  43.  
  44. if (e == 0) return 1;
  45.  
  46. ans = compModExp(b, e / 2, m);
  47. ans = (ans * ans) % m;
  48. if ((e % 2) == 1) ans = (ans * b) % m;
  49.  
  50. return ans;
  51. }
  52.  
  53.  
  54. /*
  55. * Compute S
  56. */
  57. double S(int j)
  58. {
  59. double s = 0.0; // Summation of Total, Left
  60. double t; // Each term of right summation
  61. int r; // Denominator
  62. int k; // Loop index
  63. double EPS = 1.0e-17; // Loop-exit accuration of the right summation
  64.  
  65. // Left Sum (0 ... d)
  66. for (k = 0; k <= n; k++) {
  67. r = 8 * k + j;
  68. t = (double)compModExp(16, n - k, r);
  69. t /= r;
  70. s += t - (int)t;
  71. s -= (int)s;
  72. }
  73.  
  74. // Right sum (n + 1 ...)
  75. while (1) {
  76. r = 8 * k + j;
  77. t = pow(16.0, (double)(n - k));
  78. t /= (double)r;
  79. if (t < EPS) break;
  80. s += t;
  81. s -= (int)s;
  82. k ++;
  83. }
  84.  
  85. return s;
  86. }
  87.  
  88. /*
  89. * Compute PI
  90. */
  91. void compPi()
  92. {
  93. // Time (start)
  94. t0 = clock();
  95.  
  96. // Compute Pi
  97. pi = 4.0 * S(1) - 2.0 * S(4) - S(5) - S(6);
  98. pi = pi - (int)pi + 1.0;
  99. convHex(pi, pi_hex);
  100. cout << "Hex digit: " << (char)pi_hex[0] << endl;
  101. printf("FRACTION : %.15f\n", pi);
  102. printf("10 HEX DIGITS: %10.10s\n",pi_hex);
  103.  
  104.  
  105. // Time (end of computation)
  106. t1 = clock();
  107. cout << "( TIME: " << (double)(t1 - t0) / CLOCKS_PER_SEC
  108. << " seconds )" << endl;
  109. }
  110.  
  111. // paaohjelma
  112.  
  113. int main()
  114. {
  115.  
  116. cout << "kuinka mones Piin desimaali?";
  117. cin >> n;
  118. cout << "**** PI Computation ( digit: " << n << " )" << endl;
  119. // Compute PI
  120. n = n - 1;
  121. compPi();
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment