Almkvist-Giullera formula for pi

From Rosetta Code
Revision as of 03:49, 12 October 2020 by Wherrera (talk | contribs) (→‎{{header|Python}})
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Almkvist-Giullera formula for pi is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The Almkvist-Giullera formula for calculating is based on the Calabi-Yau differential equations of order 4 and 5, which were originally used to describe certain manifolds in string theory. The formula is:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac{1}{pi^2} = \frac{2^5}{3}\sum_{0}^n\frac{6n}{n!^6(532n^2 + 126n + 9)\frac{1}{1000^2n+1}}

This formula can be used to calculate the constant π-2, and thus to calculate π.

Note that, because the product of all terms but the power of 1000 can be calculated as an integer, the terms in the series can be separated into a large integer term:

(***)

multiplied by a negative integer power of 10:

Task
  • Print the integer portions (the starred formula, which is without the power of 1000 divisor) of the first 10 terms of the series.
  • Use the complete formula to calculate and print to 70 decimal digits of precision.
Reference



Python

<lang python>import mpmath as mp

with mp.workdps(72):

   def integer_term(n):
       p = 532 * n * n + 126 * n + 9
       return (p * 2**5 * mp.factorial(6 * n)) / (3 * mp.factorial(n)**6)
   def exponent_term(n):
       return -(mp.mpf("6.0") * n + 3)
   def nthterm(n):
       return integer_term(n) * mp.mpf("10.0")**exponent_term(n)


   for n in range(10):
       print("Term ", n, '  ', int(integer_term(n)))


   def almkvist_guillera(floatprecision):
       summed, nextadd = mp.mpf('0.0'), mp.mpf('0.0')
       for n in range(100000000):
           nextadd = summed + nthterm(n)
           if abs(nextadd - summed) < 10.0**(-floatprecision):
               break
           summed = nextadd
       return nextadd
   print('\nπ to 70 digits is ', end=)
   mp.nprint(mp.mpf(1.0 / mp.sqrt(almkvist_guillera(70))), 71)
   print('mpmath π is       ', end=)
   mp.nprint(mp.pi, 71)

</lang>

Output:
Term  0    96
Term  1    5122560
Term  2    190722470400
Term  3    7574824857600000
Term  4    312546150372456000000
Term  5    13207874703225491420651520
Term  6    567273919793089083292259942400
Term  7    24650600248172987140112763715584000
Term  8    1080657854354639453670407474439566400000
Term  9    47701779391594966287470570490839978880000000

π to 70 digits is 3.1415926535897932384626433832795028841971693993751058209749445923078164
mpmath π is       3.1415926535897932384626433832795028841971693993751058209749445923078164