How did the following Java code snippet calculate Pi numbers?

What algorithm / formula uses the code below?

/** * Computes the nth digit of Pi in base-16. * * If n < 0, return -1. * * @param n The digit of Pi to retrieve in base-16. * @return The nth digit of Pi in base-16. */ public static int piDigit(int n) { if (n < 0) return -1; n -= 1; double x = 4 * piTerm(1, n) - 2 * piTerm(4, n) - piTerm(5, n) - piTerm(6, n); x = x - Math.floor(x); return (int)(x * 16); } private static double piTerm(int j, int n) { // Calculate the left sum double s = 0; for (int k = 0; k <= n; ++k) { int r = 8 * k + j; s += powerMod(16, nk, r) / (double) r; s = s - Math.floor(s); } // Calculate the right sum double t = 0; int k = n+1; // Keep iterating until t converges (stops changing) while (true) { int r = 8 * k + j; double newt = t + Math.pow(16, nk) / r; if (t == newt) { break; } else { t = newt; } ++k; } return s+t; } 

This code has already been written for us in our set of problems. I can’t find which algorithm / formula it uses, and I'm interested. I suspect this is a simple algorithm, but I cannot find the formula online based solely on this part of the code.

+4
source share
1 answer

As far as I can see, the Bailey-Borvane-Pluff algorithm calculates the nth digit pi without knowing the (n-1) th digit. The pi view is here at base 16.

Formula to calculate the Nth digit of pi

See Bailey's homepage: http://crd-legacy.lbl.gov/~dhbailey/

+10
source

Source: https://habr.com/ru/post/1434484/


All Articles