Optional Homework: Leibniz Formula

Look up the Leibniz formula for approximating pi. Implement a program that is given a number of terms as input and prints the approximation and how far it is from math.pi.

Step 1: Algorithm

In your code file, write your algorithm for how you will implement the formula. Remember, the algorithm is written in “pseudo-code” – in readable English, but detailed enough that you can use the steps to implement the code. Note that your code’s output should look like this:

Approximation with 1000 terms is 3.1425916543395442
Difference is  -0.0009990007497511222

Your algorithm must allow looping an odd number of terms – so your algorithm should not add 2 terms to the result for each loop. Also, the first term is the value 1 – which, btw, is the same as 1 / 1 (hint, hint).

Step 2: Write the code

Write the code. Use good variable names and good spacing so your code is readable. Leave your algorithm in the code as comments. Each line of the algorithm should be followed by the implementation of that line.

Extension

Implement one of the more efficient methods to approximate pi instead.