Starting from:
$29.99

$23.99

Lab01: Calculating 𝝅 Solution

1. Introduction

In this exercise, we will complete several pieces of Python programs that calculate the value of

�� and compare their performance.

2. Objectives

The purpose of this assignment is to give you experience in:

•    Using mathematical formulas in Python.

•    Converting mathematical expressions into Python expressions.

•    Refreshing knowledge on function and loops in Python.

•    Doing a little bit study on series based �� approximation.

Note: Before you start, if you are not familiar with if/else statement, for loop, while loop or function in 

Python, you are recommended to review the sample codes we covered in lecture first.  

 

3. Background

3.1. Calculating �� using series

�� is the ratio of a circle's circumference to its diameter. From the early old days,

mathematicians have been trying different methods to calculate the value of ��. Calculating �� by

using an infinite series is one popular category for �� approximation.

In this lab assignment, we will be implementing two types of series for �� calculation, namely,

the Gregory-Leibniz series and the Nilakantha series.

•    Gregory-Leibniz series is one of the simplest series to calculate the value of ��. Here is

the formula to apply:

 

𝜋   =


4         4

1   −   3   +


4         4         4

5   −   7   +   9   −


4

11   +


4

13   −


4

15   … 

 

 

•    Nilakantha series is a somewhat more complicated series to calculate the value of ��.

Here is the formula to apply:

 

𝜋   = 3 +


4

2 ∗ 3 ∗ 4   −


4

4 ∗ 5 ∗ 6   +


4

6 ∗ 7 ∗ 8   −


4

8 ∗ 9 ∗ 10   +


4

10 ∗ 11 ∗ 12   … 

These series, with every iteration, will get closer and closer to ��. In other words, the more

terms being considered in the series, the closer we can get ��. 

However, the speeds at which these series approach to �� are different. Formally, we call this

type of speed, rate of convergence. By including the same number of terms in these series, the

approximation accuracy can be quite different. Let’s have an example of including 4 terms,

where we use �� to denote the approximation for ��,

•    Gregory-Leibniz series


𝜋 =


4         4         4

1   −   3   +   5   −


4

7    = 2.8952 

 

•    Nilakantha series (where 3 in the following equation is an offset, but not the first term)

𝜋   = 3 +


4

2 ∗ 3 ∗ 4   −


4

4 ∗ 5 ∗ 6   +


4

6 ∗ 7 ∗ 8   −


4

8 ∗ 9 ∗ 10    = 3.1397 

 

As we can see above, with both four terms, Nilakantha series provides a better estimation of �� 

than Gregory-Leibniz series, since 3.1397 is much closer to �� (3.1415927) than 2.8952.

4. Assignment

When you are reading this lab assignment, you must have already logged in the Mimir

Classroom Website, joined the CSE2050-001 course and downloaded the skeleton zip file. In the zip file, together with this lab assignment, you can find a skeleton code. All you need to do is to complete the skeleton code based on the instructions and submit it to the Mimir system. 

4.1. �� calculation function

Now, open the skeleton code. You can find, at the very beginning of the skeleton code, two

functions that you need to complete. These functions use the Gregory Leibniz series and

Nilakantha series, respectively, to calculate �� and return the result. The input parameter

variable n denotes the number of terms that should be included to calculate ��. 

•    For example, if we call function GregoryLeibniz as the following,

 

GregoryLeibniz(4)

 

Then, the function should calculate �� with the first 4 terms of Gregory Leibniz series like

𝜋   =


4         4

1   −   3   +


4         4

5   −   7    = 2.8952 … 

And return value as 2.8952…

 

 

•    For another example, if we call function Nilakantha like,

 

Nilakantha(2)

 

 

Then, the function should calculate �� with the first 2 terms of Nilakantha series, 

𝜋   = 3          +


4

2 ∗ 3 ∗ 4            −


4

4 ∗ 5 ∗ 6    = 3.1333 … 



                                        Offset           1st Term                 2nd Term 

 

 

and return value as 3.1333… (Please note that the offset 3 in the equation above is not a

term in the series) 

 

 

After implementing the two functions, we can run the python script in Python Shell. The

following piece of code in the skeleton code will call the two newly implemented functions, and

print the number of terms followed by the estimated values of ��.

 

 

n = 100

for i in range(n):

    print(i+1, GregoryLeibniz(i+1), Nilakantha(i+1))

 

In the Python Shell, you should see something show up similar to the following. If your output is

different, please double check your code.

 

lines of output omitted)

 

4.2. Rate of convergence comparison

The second part of our lab is to further compare the rate of convergence of the two series. We

should add numbers of lines of code into the following corresponding positions. 

 

 

Go back to the skeleton code. The following piece of code will compute and print the minimum

number of terms required in order to approximate ��  within an accuracy of 0.001 using Gregory Leibniz series (in other words, the approximated �� value should be within 3.1415927-0.001 to

3.1415927+0.001), together with its corresponding approximated value of ��. The value of the

minimum number of terms should be stored in variable i, and its corresponding approximated

value of �� should be stored in variable result.

 

 

pi = 3.1415927  accuracy = 0.001 i = 0

diff = 1 

result = 0

### add your code here

print("GregoryLeibniz", i, result)

 

Do the same calculation to Nilakantha series and complete the last piece of code. It should print

the minimum number of terms and the approximated �� value with the same accuracy with

Nilakantha series.

 

i = 0

diff = 1

### add your code here

print("Nilakantha", i, result)

 

In this subsection, we only need to print, in total, two lines of information. Do not modify any

exiting statement or add any extra print statement.

 

5. Submit your work to Mimir

Submit your code to Mimir after you complete your code. The Mimir will automatically check

the output of your code and grade your submission. You can submit your code to Mimir up to

30 times to refresh your existing score before the submission deadline.

 

 

 

 

 

4

More products