Finding Loops & Conditions

Thomas J. Kennedy

Contents:

1 The Problem

There are multiple methods with which $\pi$ can be approximated. In this assignment… you will approximiate $\pi$ using the Leibniz formula.

Example 1: Leibniz Formula for Pi

$$ \frac{\pi}{4} = \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - \frac{1}{11} + \dots $$

Since the approximation is for $\frac{\pi}{4}$… we need to multiple the final result by $4$ to get $\pi$.

1.1 Input

This program is designed to accept used input through an interactive prompt.

Example 2: Input Prompt
How many terms should be used? 

1.2 Output

All floating point output is output to 16 decimal places.

Example 3: Sample Output

10 Terms


How many terms should be used? 10 ------------------------------------------------------------------------ Estimated: 3.2323158094055939 Known : 3.1415926535897931 Abs Error: 0.0907231558158008

20 Terms


How many terms should be used? 20 ------------------------------------------------------------------------ Estimated: 3.1891847822775961 Known : 3.1415926535897931 Abs Error: 0.0475921286878029

100 Terms

How many terms should be used? 100
------------------------------------------------------------------------
Estimated: 3.1514934010709914
Known    : 3.1415926535897931
Abs Error: 0.0099007474811983

Take note of how the approximated value for $\pi$ gets closer to the known (correct) value are more terms are used. Note that we can use up to ten million (10,000,000) terms without waiting too long (more than a few seconds) for a result.

2 Your Tasks

You will complete two (2) functions:

  1. abs_error_from_known

  2. pi_leibniz

The details of both functions, including: arguments, return values, and descriptions are included within the pydoc documentation in the provided starting code.

3 Mechanics

You can run the main program with…

python3.11 estimate_pi.py

The test code can be run with…

python3.11 -m pytest -v tests

4 Files

There are two (2) files in this assignment:

You will be working in shapes.py. Do not modify the other files.

You can access the starting files on GitHub. You may either clone the starting repository (through Git) or download a zip file.

5 Grading

Your grade will be based on the following criteria…

6 Submitting

You will submit your completed copy of estimate_pi.py as a file upload through Canvas.

Do not alter any of the other source code files, nor change the module interface.