Sample Project Report

Prof. Wright, Spring 2020

Problem Statement

I will use the following sum formula to compute approximation of π:

π=4(1-(1/3)+(1/5)-(1/7)+(1/9)-(1/11)+…)

This formula is known as the Leibniz formula for π, and can be obtained from the Maclaurin series for the arctan function.

I will investigate how the number of correct digits in the approximation of π depends on the number of terms used in the sum.

Implementation

First, I use a loop with a Print statement to generate the terms of the sum. This is just a warm-up that helps me write the following code correctly.

Do[Print[ (-1)^i/(2i+1) ],{i,0,5}]

Seeing that the terms above are correct, I will add them up and multiply by 4. I will put this code in a module that accepts the number of terms n as an argument and returns the sum.

computeSum[n_]:=Module[{sum=0},
Do[sum+=(-1)^i/(2i+1),{i,0,n-1}];
4*sum
]

Note that computeSum returns a fraction, which we can convert to a decimal number:

computeSum[100]

N[computeSum[1000],50]

Counting Correct Digits

To find the number of correct digits past the decimal point, I will write another module. This module also requires a parameter n that specifies the number of terms of the sum to use:

correctDigits[n_]:=Module[{},
diff=Abs[computeSum[n]-Pi];
Floor[-Log[10,diff]]  (* this returns the number of correct digits *)
]

I will check to make sure that correctDigits works. The sum with 800 terms has two correct digits after the decimal:

computeSum[800] //N

correctDigits[800]

However, the sum with 1000 terms also has two correct digits after the decimal, but correctDigits[1000] returns 3. It seems we may be off by one sometimes. For simplicity, we will proceed with the correctDigits module as defined above, with further discussion in the Limitations section below.

computeSum[1000] //N

correctDigits[1000]

Now I will plot of the number of correct digits for various values of n. I will let n take values from 100 to 10,000, with a step size of 100.

nDigits=Table[{n,correctDigits[n]},{n,100,10000,100}]

ListPlot[nDigits,AxesLabel->{"terms in sum","correct digits"}]

We see the number of digits increase at n=1000, but the plot is not very interesting. Let’s try n from 200 to 200,000, with a step size of 200.

nDigits=Table[{n,correctDigits[n]},{n,200,200000,200}];

This took a long time to run! Now for a plot:

ListPlot[nDigits,AxesLabel->{"terms in sum","correct digits"}]

We see increases in the number of digits at about n=10000 and n=100000. Thus, it looks like we have to increase the number of terms by a factor of 10 to obtain each additional correct digit.

Runtime

How long does it take computeSum to run? I will write a module that returns the number of seconds required to compute the sum using n terms.

runtime[n_]:=Module[{},
Timing[computeSum[n]][[1]]
]

I will make a plot of runtime vs number of terms.

times=Table[{n,runtime[n]},{n,1000,100000,1000}];

ListPlot[times,AxesLabel->{"number of terms","time (seconds)"}]

It looks like the runtime might be increasing quadratically with the number of terms.

Discussion

Conclusions and Conjectures

As shown in the plots above, the number of correct digits increases by one at about n=1000, n=10000, and n=100000.  It looks like computing each additional digit of π requires about ten times as many terms as the previous digit. I conjecture that this pattern holds as n increases, though I only tested values of n up to 200,000.

Thus, the Leibniz sum formula converges very slowly to π. This does not seem to be a practical way of computing lots of digits of π.

Limitations and Extensions

As mentioned above, sometimes my computeDigits module does is off by one. Here is an example:

N[computeSum[1000],20]
N[Pi,20]
correctDigits[1000]

My correctDigits function says there are three correct digits after the decimal point, but there are only two. To investigate this, I find the difference between the exact and computed values of π:

N[Pi-computeSum[1000],20]

This difference is very slightly less than 0.001. If two numbers differ by less than 0.001, then we might assume that they have the same first three digits after the decimal point, but this is not always the case, as shown in this example.

To improve this project, I would like to find a way to improve my computeSum module to fix the off-by-one error discussed above.

Another extension for future work is to compare the Leibniz sum with other methods of computing digits of π. I would like to find methods that compute digits of π much more quickly than the Leibniz sum formula.