Activity: For loops & Pi: Archimedes style

  1. Open a new .docx document to put your answers for this Activity. This is the file you will be submitting to Moodle.

NOTE: You will also need to create a .py file in Thonny to do some of this activity, but you WILL NOT need to submit this to Moodle.

Part 1: For loops

  1. Using a for loop and an accumulator variable, compute the sum of the first 50 numbers. Print the result.

Copy and paste your python program into the word doc and state the answer that you got for the sum.

Part 2: Archimedes

Section A: Polygon drawing program.

Copy and paste this program into Thonny. Be sure to update the header after you modify the program.


# This program draws polygons with increasing numbers of sides
# to demonstrate how polygons with large numbers of sides approach a circle
#
# Author: D. Palmer
#
# Date: September 8, 2022

import turtle

t=turtle.Turtle()

t.speed('fastest')

# This function draws a polygon with a parameter specified number of sides
def drawPolygon(numSides):
    t.setheading(90)
    t.penup()
    t.goto(-300,0)
    t.pendown()
    for sides in range(numSides):
        t.forward(50)
        t.right(360/numSides)
   
# draws polygons of increasing numbers of sides to demonstrate the trend towards
# a circle
drawPolygon(3)
drawPolygon(4)
drawPolygon(5)
drawPolygon(6)
drawPolygon(7)
drawPolygon(8)
drawPolygon(9)
drawPolygon(10)
drawPolygon(15)
drawPolygon(20)
drawPolygon(30)
  1. This program only shows polygons with 3-10 sides and also ones with 15, 20, and 30 sides. Rewrite the program using a for loop to draw ALL the polygons from 3 to 30.
  2. Cut and paste your modified program into the Word document.

Section B: Approximating Pi Using Archimedes’ Algorithm

  1. The function below approximates the number pi by using the Archimedes method for an octagon. Call it to compute the value of pi. Print the result.

Copy and paste this program below into Thonny.

   def archimedesOctagon():
      side = 2 * math.sin(math.radians(45/2.0))
      polygonCircumference = 8 * side
	  return (polygonCircumference / 2)
  1. Write a function to produce an approximation for pi using a decagon (10-sided polygon). It should be very similar to the octagon version, but the angle should be 36 instead of 45. Compare its approximation to the octagon.

  2. Copy and paste your modified program into the document. Write a short answer (in the document) explaining your findings.

  3. Copy and paste the program below into Thonny:

# This program has a function that calculates Pi using Archimedes approximation
# The function mathematically creates a polygon of a parameterized number of sides
# and then uses that value to algebraically approximate Pi
#
# Author: D. Palmer
#
# Date: Sept. 8, 2022
import math

def archimedesPiApprox(numsides):
    circApprox = (numsides * 2 * math.sin(math.radians(360/(2*numsides))))

    piApprox = circApprox / 2
    return piApprox

print("Pi with 3 sides: ", archimedesPiApprox(3))
print("Pi with 4 sides: ", archimedesPiApprox(4))
print("Pi with 5 sides: ", archimedesPiApprox(5))
print("Pi with 6 sides: ", archimedesPiApprox(6))
  1. By trying different values, find the SMALLEST number of iterations to give pi correctly for the first 4 decimal values: 3.1415

  2. Modify this program to use a loop to generate ALL approximations of pi between 3 and the result you found for the previous question.

  3. Cut and paste this program into your Word document.

How to submit

Make sure have answered all the questions. Submit your document to Moodle.