Close

Nifty math trick with hierarchical query

Using the algorithm discovered by David Bailey, Peter Borwein, and Simon Plouffe, you can easily generate values of pi with a simple recursive query…

SQL> SELECT d, to_char(bbp, rpad('0.',55, '9')) bbp,
  2    TO_CHAR(
  3       SUM(bbp) OVER (ORDER BY d),
  4       '9.' || RPAD('9', d - 1, '9')
  5    ) pi
  6    FROM (SELECT LEVEL d,
  7                   POWER(16, -(LEVEL - 1))
  8                 * (  4 / (8 * (LEVEL - 1) + 1)
  9                    - 2 / (8 * (LEVEL - 1) + 4)
 10                    - 1 / (8 * (LEVEL - 1) + 5)
 11                    - 1 / (8 * (LEVEL - 1) + 6))
 12                     bbp
 13            FROM DUAL
 14          CONNECT BY LEVEL <= 38);

  D BBP                                                          PI
--- ------------------------------------------------------------ ----------------------------------------
  1  3.13333333333333333333333333333333333333000000000000000      3.
  2  0.00808913308913308913308913308913308913308800000000000      3.1
  3  0.00016492392411510058568882098293862999745400000000000      3.14
  4  0.00000506722085385878489326765188834154351396000000000      3.142
  5  0.00000018789290093772001666738508843772001666720000000      3.1416
  6  0.00000000776775121517735681309382263783112139415700000      3.14159
  7  0.00000000034479329305086272635969401468053759158800000      3.141593
  8  0.00000000001609187715553700527429096273205488602519000      3.1415927
  9  0.00000000000077957029540010122791277882390414359723220      3.14159265
 10  0.00000000000003887115259909751224518898399125507993590      3.141592654
 11  0.00000000000000198322539359813099744403743678780728487      3.1415926536
 12  0.00000000000000010309712169788873230460615359913961954      3.14159265359
 13  0.00000000000000000544347406057178666434000298497848856      3.141592653590
 14  0.00000000000000000029121117943841783833432916474186319      3.1415926535898
 15  0.00000000000000000001575498009770082070311785298679383      3.14159265358979
 16  0.00000000000000000000086069263270039599262676755656370      3.141592653589793
 17  0.00000000000000000000004742046744556226855262717169064      3.1415926535897932
 18  0.00000000000000000000000263228669401317585860675420461      3.14159265358979324
 19  0.00000000000000000000000014709093902773314327264205417      3.141592653589793238
 20  0.00000000000000000000000000826833002827638605906177487      3.1415926535897932385
 21  0.00000000000000000000000000046727110163528570518908320      3.14159265358979323846
 22  0.00000000000000000000000000002653485901449924370022705      3.141592653589793238463
 23  0.00000000000000000000000000000151345479607531561281484      3.1415926535897932384626
 24  0.00000000000000000000000000000008666828560347770253967      3.14159265358979323846264
 25  0.00000000000000000000000000000000498130681533106648748      3.141592653589793238462643
 26  0.00000000000000000000000000000000028727019197415804591      3.1415926535897932384626434
 27  0.00000000000000000000000000000000001661838244489253463      3.14159265358979323846264338
 28  0.00000000000000000000000000000000000096413362728707559      3.141592653589793238462643383
 29  0.00000000000000000000000000000000000005608493642017818      3.1415926535897932384626433833
 30  0.00000000000000000000000000000000000000327065934758869      3.14159265358979323846264338328
 31  0.00000000000000000000000000000000000000019117544868650      3.141592653589793238462643383280
 32  0.00000000000000000000000000000000000000001119879586043      3.1415926535897932384626433832795
 33  0.00000000000000000000000000000000000000000065734562356      3.14159265358979323846264338327950
 34  0.00000000000000000000000000000000000000000003865856221      3.141592653589793238462643383279503
 35  0.00000000000000000000000000000000000000000000227760336      3.1415926535897932384626433832795029
 36  0.00000000000000000000000000000000000000000000013441452      3.14159265358979323846264338327950288
 37  0.00000000000000000000000000000000000000000000000794528      3.141592653589793238462643383279502884
 38  0.00000000000000000000000000000000000000000000000047036      3.1415926535897932384626433832795028842

38 rows selected.

SQL>

Leave a Reply