Tuesday, November 10, 2009

Calculation of PI

http://www.reddit.com/r/programming/comments/9uxjo/dear_progg... on Twitpic

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)

9 comments:

EdwinaHugh said...

情色a片flashxyz情色視訊1539情色視訊chess333情色視訊jackiepan情色視訊jiafu情色視訊spice情色視訊welljohn情色論壇lychee視訊交友ige視訊交友loveliness情色a片flashxyz情色視訊1539情色視訊chess333情色視訊jackiepan情色視訊jiafu情色視訊spice情色視訊welljohn情色論壇lychee視訊交友ige視訊交友loveliness情色a片flashxyz情色視訊1539情色視訊chess333情色視訊jackiepan情色視訊jiafu情色視訊spice情色視訊welljohn情色論壇lychee視訊交友ige視訊交友loveliness情色a片flashxyz情色視訊1539情色視訊chess333情色視訊jackiepan情色視訊jiafu情色視訊spice情色視訊welljohn情色論壇lychee視訊交友ige視訊交友loveliness

雪糕 said...

You may be only one person in the world, but you may also be the world to one person.............................................

開心唷 said...

wonderful...................................................

宥軒 said...

a片性愛av女優性愛台灣性愛自影片性愛巴士線上看sex性愛洪爺線上免費直播免費男同志網免費性圖片免費東洋影片免費的av片免費的片免費的色情卡通免費直撥網免費直撥影片免費直播短片免費長片分享區免費咪咪影片網免費是訊免費洪爺色情免費洪爺色情網免費性愛觀看影片免費性愛影片分享區免費性愛動畫免費男做愛影片免費男影片免費男優免費亞洲影片免費性小說免費性片下載免費性交貼圖成人文學網a18禁女生遊戲區視訊聊天室ut聊天室

寧年寧年 said...

到處盡心,即為快事;舉步踏實,便是坦途。 ..................................................

郭美娟 said...

你不能改變容貌~~但你可以展現笑容......................................................

火芬 said...

大奶妹貼圖區0204性影片觀賞露點自拍淫婦女生如何自慰色情站成人笑話av激情網愛視訊美女淫蕩av成人色情電話辣妹視訊聊天性關係情色vcd自慰圖淫美成人論壇台灣色情論壇成人聊天室自拍裸女貼圖視訊成人免費a片影片av成人網成人色情色情台灣辣妹小穴太太陰毛色情訊息裸女自拍色情影片a片論壇性愛技巧美女脫胸罩情色天堂av寫真色情視訊聊天做愛視訊成人影片床上戲情色聊天網火辣情色台灣女優性愛秘笈台灣av女優手淫自慰影片

志平 said...

^^ 謝謝你的分享,祝你生活永遠多彩多姿!........................................

AronArendt0418 said...

看看blog調整心情,又要來繼續工作,大家加油........................................