CS 100

Logo

Scientific Computing

Activity 7 - Pi: Archimedes style

  1. Open a new file in Thonny. Save it to your CS100/ch2 folder as pi.py.

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

  3. The function below computes the number Pi by using the Archimedes method for an octagon. Copy it to the top of your file. Call it to compute the value of Pi. Print the result.

    def archimedesOctagon():
       side = 2 * math.sin(math.radians(45/2.0))
       polygonCircumference = 8 * side
       return (polygonCircumference / 2)
    
  4. Create another function with the function signature def archimedes(numSides). This function is more general than the one above as it will compute Pi using the Archimedes method with any polygon you wish. Hint: You can use the above function as a model.

  5. Try your new function with the following function calls.
    print('Octagon: ', archimedes(8))
    print('Dodecadon: ', archimedes(12))
    

    Which is better?

  6. Add the following for loop in your program and run it. What is this for loop accomplishing?

    for sides in range(8, 100, 8):
       print(sides, archimedes(sides))
    
  7. If you finish early, use a for loop to approximate Pi using the Archimedes method with a number of sides which increases by a multiple of 10 in each iteration of the loop.

How to submit

Make sure your code works. Submit your pi.py file to Moodle.