Advertisement
am1x

sumgammaadd002

Feb 3rd, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include <quadmath.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <inttypes.h>
  5. #include <math.h>
  6. #include <vector>
  7.  
  8.  
  9. typedef __float128 real;
  10.  
  11.  
  12. template <typename T> struct Adder {
  13. std::vector<T> v;
  14.  
  15. void reset() {
  16. v.clear();
  17. v.push_back(0);
  18. }
  19.  
  20. Adder() { reset(); }
  21.  
  22. T sum() const {
  23. T res = v[0];
  24. size_t l = v.size();
  25. for (size_t i = 1; i < l; i++)
  26. res += v[i];
  27. return res;
  28. }
  29.  
  30. void operator+= (T x) {
  31. size_t i = 0;
  32. for (i = 0; v[i]; i++) {
  33. x += v[i];
  34. v[i] = 0;
  35. }
  36. v[i] = x;
  37. if (i + 1 == v.size())
  38. v.push_back(0);
  39. }
  40.  
  41. };
  42.  
  43.  
  44. int main()
  45. {
  46. uint64_t n = 100000000;
  47. Adder<real> a;
  48. for (uint64_t i = 2; i <= n; i++) {
  49. real x = i;
  50. real y = expm1q(logq(x) / x) / x;
  51. a += y;
  52. }
  53. real s = a.sum();
  54.  
  55. {
  56. real x = n + 0.5q;
  57. real lx = logq(x);
  58. real y1 = (lx + 1) / x;
  59. real y2 = (2 * lx * (lx + 1) + 1) / (2 * 4 * x * x);
  60. real y3 = (((9 * lx + 9) * lx + 6) * lx + 2) / (6 * 27 * x * x * x);
  61. real y4 = ((((32 * lx + 32) * lx + 24) * lx + 12) * lx + 3) / (24 * 128 * (x * x) * (x * x));
  62. real y5 = (((((625 * lx + 625) * lx + 500) * lx + 300) * lx + 120) * lx + 24) / (120 * 3125 * x * (x * x) * (x * x));
  63. real xrx = expm1q(lx / x);
  64. real yd = (x * xrx + (xrx + 1) * (lx - 1)) / (24 * x * x * x);
  65. s += y1;
  66. s += y2;
  67. s += y3;
  68. s += y4;
  69. s += y5;
  70. s -= yd;
  71. }
  72.  
  73. printf("n=%lu, %.36Qf\n", n, s);
  74. return 0;
  75. }
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement