Create a file called hw03
Store it in your CS100/hw
folder.
You are to write 5 functions on the topics of functions, loops, and Archimedes’ approximation of Pi.
You should put all 5 functions in the file you created and submit that to Moodle.
5, 8, 11, 14, 17, 20, 23, 26, 29 for a summation of 153
For another example, if the parameters are 12, 2 ,16 the function should add the following values:
12, 14, 16 for a summation of 42
The output of the program should look like this (for inputs 5, 30, 3):
Computing the sum of the following numbers:
5
8
11
14
17
20
23
26
29
The sum is: 153
1/1 + 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + …
The function should take one parameter that specifies how many of the fractions should be added. The first fraction is always 1/1.
If the function is called with 3, the output should be:
Adding 1 / 1 to the total, which is now: 1.0
Adding 1 / 2 to the total, which is now: 1.5
Adding 1 / 4 to the total, which is now: 1.75
Final result is: 1.75
If the function is called with 10, the output should be:
Adding 1 / 1 to the total, which is now: 1.0
Adding 1 / 2 to the total, which is now: 1.5
Adding 1 / 4 to the total, which is now: 1.75
Adding 1 / 8 to the total, which is now: 1.875
Adding 1 / 16 to the total, which is now: 1.9375
Adding 1 / 32 to the total, which is now: 1.96875
Adding 1 / 64 to the total, which is now: 1.984375
Adding 1 / 128 to the total, which is now: 1.9921875
Adding 1 / 256 to the total, which is now: 1.99609375
Adding 1 / 512 to the total, which is now: 1.998046875
Final result is: 1.998046875
Rewrite the code from #5 on HW 2. Instead of drawing a specific 6x6 grid, write a function that takes two parameters, the number of rows and the number of columns. Whatever values are input, the function should generate a grid of the size provided. If you pass in the two parameters as 6 and 6, you should get the same drawing as for #5 on HW 2. If you pass in 3 and 10, you should get 3 rows that each contain 10 columns.
Copy and Paste the program below which draws a red circle like the one in class today. You will need to modify this program to draw a diameter and an inner square and an outer square. (Like the program in class today.) You will need to use trial and error to figure out the length and position for the diameter and both squares. Use the values that you determine to fill out the printing part of the program.
# This program demonstrates Archimedes' approximation of Pi using a 4-sided polygon
#
# Author: D. Palmer
#
# Date: September 8, 2022
import turtle
import math
t=turtle.Turtle()
t.speed('fastest')
t.penup()
t.goto(0,300)
# draw circle in the center of the drawing canvas
t.pendown()
t.pencolor("red")
t.width(3)
# This loop draws the "circle" to the screen
# DO NOT CHANGE THIS LOOP IN ANY WAY
for circle in range(361):
#t.forward(3.0545) <--- this was the value from in class on 9/9/2022
t.forward(4.9744)
t.right(1)
# add code here to draw the diameter
t.pencolor("black")
#draw a square inside the circle
def drawSquare(x,y,side):
t.penup()
t.goto(x, y)
t.pendown()
for square in range(4):
t.forward(side)
t.right(90)
# set the parameters to draw a square inside the circle
#drawSquare()
#set the parameters to draw a square outside the circle
#drawSquare()
# Modify all the statements here to match this new circle being drawn
print("Circle's diameter is: ", 350) # <-- change this line
print("Circle's radius is: ", 175) # <-- change this line
print()
print("Inner Square's side is: ", 249) # <-- change this line
print("Inner Square's perimeter is: ", 4*249) # <-- change this line
print()
print("Outer Square's side is: ", 353) # <-- change this line
print("Outer Square's perimeter is: ", 4*353) # <-- change this line
print()
print("The inner perimeter and outer perimeter are lower and upper bounds for the circumference") # <-- change this line
print()
print("Therefore, the Circle's circumference is greater than ", 996, " and less than ", 1412) # <-- change this line
print()
circumference = (996 + 1214)/2 # <-- change this line
print("As an approximation, we'll use a value half between: ", circumference)
print("The formula for a circle's 'perimeter' is: Circumference = 2*Pi*Radius")
print("Solving for Pi yields: Pi = Circumference / (2*Radius)")
print()
piApprox = circumference / (2 * 175) # <-- change this line
print("to calculate Pi: ", piApprox)
print()
print("The difference from the actual value of Pi is: ", piApprox - math.pi)
The formula for computing the perimeter of an INSCRIBED polygon is in the code and presented here:
circApprox = (numsides * 2 * math.sin(math.radians(360/(2*numsides))))
The formula for computing the perimeter of a cCIRCUMSCRIBED polygon is very similar:
circApprox = (numsides * 2 * math.tan(math.radians(360/2*numsides))))
a. Replace the inner polygon code with the outer polygon code and determine the minimum number of iterations to approximate pi to the 4th decimal position. Like we did in class.
b. Now rewrite the program using BOTH the inner and the outer approximations, add them together and divide by 2, find the minimum number of iterations to approximate pi to the 4th decimal position.
c. Explain your observations in the comments to the program.
In order to receive any credit, your program must run without syntax errors in Thonny. Thonny reports syntax errors in red in the shell area; if Thonny outputs an error, your code isn’t usable. Code that doesn’t run is just text - it’s not a program. It’s an important part of programming and problem solving to come up with a solution that can be shared with others.
Your program doesn’t have to be perfect to be eligible for credit! It’s okay to have some semantic errors. Semantic errors don’t stop your program from running, but they cause your program to produce an unexpected or incorrect result. For example, if your program is supposed to calculate the sum of the first 100 numbers, but instead it accidentally calculates the sum of the first 101 numbers, that would be a semantic error. Semantic errors may result in partial credit depending on how close your solution is.
Make sure that all of the code that you wrote for this assignment has been included in your .py file. Make sure that you have named in hw03