Today I was drawing answers to a quiz using boxes that stood for binary representations of the answers and realized that any regular polygon is composed of triangles. I thought back to a question I saw on reddit where someone had challeneged the readers to determine the radius of a circle without knowing what PI was.

I figured this could be a way to find the perimeter of any polygon using its "radius". Knowing that PI is the ratio of the circumference to the diameter , a polygon with the number of sides approaching infinity will have a ratio of perimeter to diameter approximately equal to PI.

I started with a square. Its composed of 4 triangles and I used the law of sines to find the perimeter. The angle of the center is 360/4 (where 4 is n for squares being 4 sided) then other 2 angles are (180-(360/4))/2 or half the remaining degrees left in the triangle. If the radius is 1 then the perimeter of the square in terms of the law of sines is (sin(90)*4)/sin(135). If we continue this for a hexagon we find that the perimeter of a hexagon with radius 1 is 6. (sin(60)*6)/sin(60)).

We can take the limit of this function as the number of sides approaches infinity with the radius of 1. We take the result and divide it by the diameter of the "circle" which would be 2.

Here is the resulting code.

dr=lambda{|x| x*(Math::PI/180)} #degrees to radians (cuz Math.sin won't take degrees)

p=lambda{|r,n| (r*Math.sin(dr[360.0/n])*n)/Math.sin(dr[(180-(360.0/n))/2.0])}

pitime=lambda{|r|

last=0

4.upto(1.0/0) {|x| #from the perimeter of a square to infinity

y=p[r,x]/(r*2) #calculate ratio of perimeter to diameter

break if y==last

last=y

}

last

}

puts pitime[1.0]

>> 3.14159265341633

(Took 172599 iterations in 1.5seconds to get that number)