Python
13.2 Gregory's Series

 


Gregory's Series: Here we're showing a larger calculation: sums of Gregory's series, using the formula below. These sums are computed with up to 100-digit accuracy (and much higher accuracy at the end of this section), using Python, with the mpmath extension. (I've carried out many of these high precision calculations on Mathematica and clisp, getting exactly the same results.)

mpmath: More Sample Calculations
Ordinary Python Python with mpmath
# greg.py: Gregory's series
#   for doubles
from __future__ import print_function




N = 100
sum = 0.0
sign = 1
for k in range(0,N):
    term = 1.0/(2.0*k + 1.0)
    sum = sum + sign*term
    sign = -sign
print(N, sum*4)
# greg_mp.py: Gregory's series,
#   multi-precision
from __future__ import print_function
from mpmath import *

mp.dps = 100
print("Precision: ", mp.dps)
N = 5000
sum = 0
sign = 1
for k in range(0,N):
    term = 1/(mpf(2)*k + 1)
    sum = sum + sign*term
    sign = -sign
print(N, sum*4)

Notice that in the program at the right above, I only needed to force one constant to be multi-precision, and then mpmath forced each other variable and operation to also be multi-precision. (There is no particular value in relying on this, and it may make more sense to declare everything multi-precision.)

Below, the blue digits are initial correct ones, the red digits are incorrect, and the green digits are correct digits that come after at least one incorrect one.

   Gregory's Series, up to 10 million terms, up to 100-digit accuracy.
N=       10 3.041839618929402211135957
N=      100 3.131592903558552764307414238276920516403
N=     1000 3.140592653839792925963596502869395970451
N=    10000 3.1414926535900432384595183833748153787870136427443
N=   100000 3.1415826535897934884626433520295028937284193939649495759086359919592690
N=  1000000 3.1415916535897932387126433832791903841971703525001058155647883423571533203480491438917188683707422603
N= 10000000 3.1415925535897932384628933832795028810721693993752011334749445868976601562867023677686597593566434351
Exact:pi    3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
                       1         2         3         4         5         6         7         8         9         0
              1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

Items of Interest or for study:

  • It was quick and easy to convert the ordinary Python program to the arbitrary precision form.

  • Notice the ordinary integer variables mixed in on the right: N, sum, and k.

  • This series converges very slowly, so that with 10000000 terms we only get 6 correct digits of pi to the right of the decimal point. However, amazingly, the green digits show a large number of correct digits after incorrect ones. There are insanely too many for this to be random chance, although any given random digit ought to be correct with probability 1/10. Thus the isolated correct digits above, and even pairs of correct digits (with probability 1/100), might be correct by chance.

 


Explanation for the above results: These results suggest the existence of a sequence of correction terms that would make the final sum much more accurate. (See Gregory's Series for further explanation.)

In fact, the phenomenon we are observing is an artifact of the particular terms in Euler's famous formula for  pi/4:

One must first realize that the formula deals with so-called "real" numbers. Inside the computer, one works with floating point numbers, as a partial and finite representation of some real numbers. In most computers the representation is in binary, in a special hexadecimal form in Intel machines, and in actual binary in mpmath. In some calculators, especially historically, the representation has used decimal numbers. In spite of this, the strange results in the table above, where there are more correct digits of pi than one would expect, is a result of displaying certain results in decimal form, and in adding up a number of terms that is a power of 10. (These weird results were first noticed in 1988.)

In the formula above, the sum comes first. Then the next term is (-1)N/N. If N is a power of 10, say, N = 10000, then that term will just subtract 1 from the 4th place of the decimal expansion, and make no other change. The table above shows that is exactly what has happened.

The second term, with its pesky extra 4 in the denominator, will end up subtracting the digits "025" (because of the "one-fourth") from the decimal value, starting in the 12th digit, and make no other change. Let's look at the numbers involved in detail (this example is a bit weird, because adding in "25" changes the previous 2 digits because of carries):

Exact:pi    3.1415926535897932384626433832795028841971693993751
          - 0.000100000000000000000000000000000000000000000000
          = 3.1414926535897932384626433832795028841971693993751
          + 0.0000000000002500000000000000000000000000000000
          = 3.1414926535900432384626433832795028841971693993751
If you look at the earlier table, you can see that on each line, the first red digit is the result of subtracting a "1" from pi at the proper place, and the next pair of red digits results from adding "25" at the proper place.

With higher powers of 4 and with a numerator not equal to 1, each new term makes successively larger contributions to the total, though starting further and further out in the decimal expansion and always ending in non-terminating 0's.

I emphasize that these results depend on using decimal (base 10) representations, and on a power of ten as the number of terms added up. One gets similar and even more impressive results for 5 followed by a power of ten, as shown in the table below, and less impressive such results for, say, 2, 4, or 8 followed by a power of ten, and for other numbers of terms.

 


Gregory's Series, more sums: Here I'm showing additional sums of Gregory's series, computed with up to 250-digit accuracy and up to 10 billion terms. I'm also including those with a five followed by a power of ten. I have no intuition about why starting with a five gives better results. This single calculation took about 25 days on my slow and old PC at home.

   Gregory's Series, up to 10 billion terms, up to 250-digit accuracy.
N=       10 3.0418396189294022111
N=       50 3.121594652591010478513182974309
N=      100 3.131592903558552764307414238276920516403
N=      500 3.139592655589783238584640613380539479065
N=     1000 3.140592653839792925963596502869395970451
N=     5000 3.14139265359179323836264339547950011419817981883455
N=    10000 3.1414926535900432384595183833748153787870136427443
N=    50000 3.1415726535897952384626423832795041041971666293751159251748905370082151
N=   100000 3.1415826535897934884626433520295028937284193939649495759086359919592690
N=   500000 3.141590653589793240462643383269502884197291399375103050974944693349816400880678999026756787
N=  1000000 3.14159165358979323871264338327919038419717035250010581556478834235715332034804914389171886837074226037240467222
N=  5000000 3.14159245358979323846464338327950278419716939938730582097494182230781640729662899862749427234211746670411008612545206374757482054463804148934888574594
N= 10000000 3.14159255358979323846289338327950288107216939937520113347494458689766015628670236776865975935664344
N= 50000000 3.141592633589793238462645383279502884196169399375105822194944592307813636286208998638139025342117013926848086513681028609093840731248153231773457001637300376707907627665808295984186
N=100000000 3.14159264358979323846264363327950288419713814937510582098447584230781640087605274862803975903352331797554953915000106856
N=500000000 3.1415926515897932384626433852795028841971693893751058209749447143078164062862062286280348253422181099821480865078767766470938450082725442317253206251041911174550938620528205203648172681478990 
 1000000000 3.14159265258979323846264338352950288419716939906260582097494459326094140628620899321787857534211711731906214901328164679235751648456275026035
 5000000000 3.141592653389793238462643383281502884197169399375005820974944592307828606286208998628032055342117067982149096933282306647093304056550582231725758130090481117449896272459038521106040620558037154929641222052335925667320423616338406250683876332559784899
10000000000 3.141592653489793238462643383279752884197169399375102695974944592307816501598708998628034819931960817982148087006651447272093844543565108598912859420296509742938565349743028586870940504915337747136573980677159985906941251164421717934833490702954844631
Exact:pi    3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091
                                                                                                                 1         1         1         1         1         1         1         1         1         1         2         2         2         2         2         2>
                       1         2         3         4         5         6         7         8         9         0         1         2         3         4         5         6         7         8         9         0         1         2         3         4         5
              1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890

Gregory's series calculates pi/4, not pi. I just multiplied by 4 at the end. It might be true that the calculation of pi/4 gives more correct digits (of pi/4, of course, not pi) than the values I've been presenting. But in terms of initial correct digits, pi is much better than pi/4, as the data below shows. All but the first of the extra terms in Euler's formula have at least one 4 in the denominator, so it makes some sense that multiplying by 4 would help. I thought if one 4 helped, I'd see about two 4s, calculating pi*4. However, one can see in the table below that pi*4 is a bit worse than pi.

Gregory's Series, comparing calculation of pi/4 with that of pi and p*4
500000        0.7853976633974483101156608458173757210493228498437757627437361733374541002201697497566891968258335104
Exact, pi/4   0.7853981633974483096156608458198757210492923498437764552437361480769541015715522496570087063355292670
500000        3.141590653589793240462643383269502884197291399375103050974944693349816400880678999026756787303334041
Exact, pi     3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
500000        12.56636261435917296185057353307801153678916559750041220389977877339926560352271599610702714921333617
Exact,pi*4    12.56637061435917295385057353311801153678867759750042328389977836923126562514483599451213930136846827


N = 1000000   0.785397913397448309678160845819797596049292588125026453891197085589288330087012285972929717092685565
Exact, pi/4   0.7853981633974483096156608458198757210492923498437764552437361480769541015715522496570087063355292670
N = 1000000   3.14159165358979323871264338327919038419717035250010581556478834235715332034804914389171886837074226
Exact, pi     3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

N=10000000    0.785398138397448309615723345819875720268042349843800283368736146724415039071675591942164939839160859
Exact, pi/4   0.7853981633974483096156608458198757210492923498437764552437361480769541015715522496570087063355292670
N=10000000    3.14159255358979323846289338327950288107216939937520113347494458689766015628670236776865975935664344
Exact, pi     3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068

 

Utility program to create html code: Here is a Python program to create the html code above with blue, red, and green highlights. (In some cases, I did the highlighting by hand and didn't redo it, so there may be a few mistakes.) The code below compares two strings character by character in the same positions, left to right. Initial identical characters are colored blue. All differing characters are colored red. Characters that are the same but come after one or more characters that differ are colored green.

annotate.py: Highlight html with colors
# annotate.py: annotate greg display
from __future__ import print_function

def ann(np, pi):
    BLUE  = "<font color=0000dd>"
    RED   = "<font color=ff0000>"
    GREEN = "<font color=00aa00>"
    END   = "</font>"
    out   = ""
    inR  = 0 # not in red section
    inG  = 0 # not in green section
    inB  = 0 # not init in blue sect
    done = 0 # not done yet
    out += RED
    out += BLUE
    n = 0 # main index
    if np[0] == pi[0]:
        inB = 1 # initially in blue
    else:
        inR = 1 # initially in red
        out += END
    while n<len(np) and n<len(pi):
        if inB: # initially same
            out += np[n]
            n = n + 1
            if n == len(np):
                inB = 0
                done = 1
            elif np[n] != pi[n]:
                out += END
                inB = 0
                inR = 1
        elif inR: # different
            out += np[n]
            n = n + 1
            if n == len(np):
                inR = 0
                done = 1
            elif np[n] == pi[n]:
                out += GREEN
                inR = 0
                inG = 1
        elif inG: # the same
            out += np[n]
            n = n + 1
            if n == len(np):
                inG = 0
                out += END
                done = 1
            elif np[n] != pi[n]:
                out += END
                inG = 0
                inR = 1
        elif done: # all done
            break
        else:
            print("ERROR")
    out += END
    return out

npp = "3.131592903558"
pip = "3.141592653589"
res = ann(npp, pip)
print(res)
print(pip)
Input as shown, output is:  <font color=ff0000><font color=0000dd>3.1</font>3<font color=00aa00>1592</font>
90<font color=00aa00>35</font>58</font>

Above, and in the listing, the "<" character has been replaced by "&lt;", so that nothing is interpreted
as html, but a "<" character appers. Then to get the "&lt;" I just typed, I had to type: "&amp;lt;",
and to get that I had to type "&amp;amp;lt;", and so on forever ... (always one "amp;" short).

The result in html is: 3.131592903558
                       3.141592653589


Another series: Here is a different series that can be used to compute π. It converges more rapidly, but still to slowly to be useful.


Gregory's Series to Other Number Bases: For similar work using number bases other than 10, see: Other Number Bases.

(Revision date: 2018-10-10. Please use ISO 8601, the International Standard.)