'''Archimedes method for calculating value of pi''' import math def archimedes(numSides): # Approximate the value of pi by inscribing a polygon # within a circle and measuring the lengths of the sides. # The polygon will have numSides sides of length sideS. # The trick is to calculate the value of sideS. # Assume a unit circle (meaning radius is 1). # Angle B is the the angle of the triangle that has a side # as one edge and connects each end of that edge to the center. # This angle is determined by the number of sides. innerAngleB = 360.0 / numSides # We want half of B, to get a right triangle. halfAngleA = innerAngleB/2 # Then we can calculate the length of the outer side of # this triangle to be the sin(A). # Python expects radians, so convert. oneHalfSideS = math.sin(math.radians(halfAngleA)) # s is this length doubled. sideS = oneHalfSideS*2 # The circumference is just s times the number of sides. circumference = numSides * sideS # Divide by 2 because that is the diameter of a unit circle. pi = circumference/2 return pi