How to find "pi" after armageddon
Posted: Sat Aug 18, 2018 12:39 pm
After armageddon, you find yourself in a desert. Your first priority is to re-establish the value of pi (i don't know why, but that's how it is). Find a branch (a piece of wood) and draw a number of equidistant lines in the sand, the distance being precisely the length of the branch.
Then start throwing the branch in the area with the lines.
Jot down the total number of throws and the number of cases in which the branch crosses (hits) one of the lines. After a sufficient number of tries, the following quotient:
2 x total tries / hits will approximate the value of pi.
Then start throwing the branch in the area with the lines.
Jot down the total number of throws and the number of cases in which the branch crosses (hits) one of the lines. After a sufficient number of tries, the following quotient:
2 x total tries / hits will approximate the value of pi.
Code: Select all
option angle degrees
graphics ! graphics clear 1,1,1
get screen size w,h
cx=w/2 ! cy=100+w/2 ! ww=100*floor(w/100)
xo=(w-ww)/2 ! n=20 ! b=ww/n ! b2=b/2 ! e=ww-b ! e2=e/2
draw size 3 ! draw color 0,0,0
draw rect xo,100 to xo+ww,100+ww
field "result" text "" at 20,20 size ww-20,70 RO ML
field "result" font size 24
draw size 1 ! draw alpha .4
for i=1 to n-1
draw line xo,100+i*b to xo+ww,100+i*b
'draw line xo+i*b,100 to xo+i*b,100+ww
next i
draw size 1 ! draw alpha 1
for i=1 to 5000
x=cx+e2-rnd(e) ! y=cy+e2-b-rnd(e) ! ang=rnd(360)
dx=b2*cos(ang) ! dy=b2*sin(ang)
y1=y-dy ! y2=y+dy ! draw line x-dx,y1 to x+dx,y2
r=abs(floor((y1-100)/b)-floor((y2-100)/b))
tot+=1 ! pi+=r ! div=max(1,pi)
t$="total= "&tot&", hits= "&pi&chr$(10)&" pi="&2*tot/div
field "result" text t$
pause 2/i
next i
end