Finding Loops & Conditions
Thomas J. Kennedy
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 PromptHow many terms should be used?
1.2 Output
All floating point output is output to 16 decimal places.
Example 3: Sample Output10 Terms
How many terms should be used? 10 ------------------------------------------------------------------------ Estimated: 3.2323158094055939 Known : 3.1415926535897931 Abs Error: 0.090723155815800820 Terms
How many terms should be used? 20 ------------------------------------------------------------------------ Estimated: 3.1891847822775961 Known : 3.1415926535897931 Abs Error: 0.0475921286878029100 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:
-
abs_error_from_known
-
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:
estimate_pi.py
test/test_estimate_pi.py
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…
- test 000 - code passes
test_abs_error_from_known
- test 001 - code passes
test_pi_leibniz_up_to_10_terms
- test 002 - code passes
test_pi_leibniz_up_to_100_terms
- test 003 - code passes
test_pi_leibniz_up_to_10_000_terms
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.