Assignment 2
1. 📝 Worth: 6%
2. 📅 Due: March 14, @Midnight
3. 🕑 Late submissions: 10% penalty per late day. Maximum of 3 late days allowed.
4. 📥 Submission:
Submit your .py file(s) individually on Gradescope
Please use the provided files WITHOUT renaming them.
Fill the docstring header at the top of the .py file with your name, section and date.
🎯 Learning Objectives
Break down a problem into smaller solvable steps
Implement an existing algorithm using a for-loop
Testing an algorithm for correctness
Use turtle graphics to visualize the process defined by the algorithm.
Context - Estimating Pi
Pi ( ) is defined as the circumference of a circle ( ) divided by its diameter( )
Back in the day, before there was trigonometry and algebra, there was geometry! Archimedes invented
an algorithm for estimating pi.
Pi ( ) is defined as the circumference of a circle ( ) divided by its diameter( )
How can we measure the value of ? We could just use a rope and a measuring stick.
But what if we want more accuracy? Archimedes decided to approximate the value of using
equilateral triangles (which fit exactly within a circle) and forming a hexagon as shown below:
1
The perimeter of the six sided figure is , and the radius of the circle is , thus:
To increase the accuracy, Archimedes doubled the number of sides of the polygon as such:
So now with a sided polygon, we have:
But how do we calculate the value of the new value of side using the old value ? Simple geometry
using Pythagoras' theorem, we can establish a relationship between the previous side and the new
side
1
s
b/2 b
y x
Side note: Archimedes calculated the results by hand up to 96 triangles, giving a lower bound to
pi.
> 3.14103195
Results of this calculation:
Number of sides: 12, pi estimate: 3.10582854
Number of sides: 24, pi estimate: 3.13262861
Number of sides: 48, pi estimate: 3.13935020
Number of sides: 96, pi estimate: 3.14103195
Number of sides: 192, pi estimate: 3.14145247
We can also visualize the drawn polygons using turtle graphics:
Algorithm (10 pts)
Implement the algorithm described below while setting the number of iterations n to 6 for now.
Remember to use meaningful variable names and define all hard-coded numbers at the beginning of
the code.
Initialize num_sides to 6.
Initialize the length of the side to 1.
(described later) draw a red circle
calculate =
loop times, each time
(described later) draw a polygon
double the number of sides
re-calculate the new length using the formulas described above. Ensure that the variable
names are meaningful when possible.
re-calculate =
print the new side and the new estimate of pi in the console
(described later) display the values on the screen
Important
You CANNOT use the math.pi constant.
You must use a for loop with the range function.
Keep the formulas in their current form to avoid precision problems.
Testing
To ensure that the algorithm is working, run the script.
Here are the first 6 iterations of the algorithm:
Iteration 1/6, num_sides = 6, pi_estimate = 3.0
Iteration 2/6, num_sides = 12, pi_estimate = 3.105828541230249
Iteration 3/6, num_sides = 24, pi_estimate = 3.1326286132812378
Iteration 4/6, num_sides = 48, pi_estimate = 3.1393502030468667
Iteration 5/6, num_sides = 96, pi_estimate = 3.14103195089051
Iteration 6/6, num_sides = 192, pi_estimate = 3.1414524722854624
Pi's estimate should improve over time.
Visualizing the algorithm
In this next step you will use turtle graphics to draw the successive polygons.
User input 1 pt
The script should allow the user to input the number of iterations, n , which determines how
many times the process of doubling the number of sides will be repeated.
Ensure that the entered value is converted to an int
Creating the red circle
We need to scale up the dimensions of the circle from 2 to a larger diameter of 200 so that it's
visible on the screen.
Create a scale variable and set its value to 100
Create a diameter variable set its value to 2
Create a Turtle() object to draw the red circle, set its color to red .
Trace the red circle using the circle() function using the radius of scale * diameter/2
Ensure the circle is centred in the middle of the screen.
Hint:
Use the penup() and pendown() functions to move the pen to the start of the circle
Draw the circle using the circle(radius) function
Drawing the polygons 6 pts
This part can be tricky because the polygons must be scaled up to be seen on the turtle window while
ensuring they are centred on the screen:
Create a Turtle object for the polygon:
polygon = turtle.Turtle()
Start by drawing a hexagon (with 6 sides) using turtle.
Ensure that the hexagon is centred on the red circle.
You must find a general way to draw a polygon of any number of sides ( num_sides ) and side
length provided by scaling the scale*side
Add this code in the algorithm's main loop.
All polygons must remain centred on the screen.
Outputting 2 pt
Use the provided functions write_pi_estimate() and write_iteration() to display the
estimate of pi with a precision of 8 digits as well as the number of repetitions as shown below:
Convergence 1 pt
Archimedes' method gradually converges to the true value of π, but this process can be time-
consuming. To optimize efficiency, we need a way to stop the loop once a "good enough" estimate is
achieved. A sufficient level of precision is reached when the change between consecutive π estimates
is less than 0.000001 (i.e., stable up to 6 decimal places). Implement a condition to monitor this
variation and terminate the loop accordingly.
Grading Rubric
Criteria Description Points
The code is well structured uses variables. The variables are
Code Quality following the naming conventions seen in class. Avoids using 5
magic numbers. The started code is NOT renamed.
Criteria Description Points
The algorithm is correctly implemented using a for loop. The
Accurate equations are correctly implemented. The estimate of pi is
10
estimation of pi calculated using Archimedes' method. The math.pi is NOT
used.
Convergence Interrupts the algorithm 1
The script draws the concentric polygons correctly and the
Visualization 6
circle.
Input The script allows the user to input the number of iterations 1
Output The script displays the estimate of pi and each iteration. 2
Adds comments to clarify code. Docsify header is completed
Documentation 1
with Name, ID, section ,etc..