Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How to Calculate Pi

Name: Anonymous 2018-10-25 23:07

Ok. I've never ever calculated Pi and always trusted the value given online. So lets check that Pi is actually 3.141592... For some reason, algorithm for calculating Pi is never given in programming textbooks. So to calculate Pi, we need to devise our own algorithm, which requires precise understand what is Pi.

Pi is length of the curve making the circle of radius 1. Therefore we also need a definition of a circle and a definition of length, so we will use Pythagoras Theorem:
1. A circle is a set of points equally distanced from a given point.
2. By Pythagoras Theorem the distance between points (a,b) and (c,d) is sqrt((a-c)^2 + (b-d)^2). That only applies to Euclidean geometry. If we take different axioms, the value of Pi could be different too, and circle would look different as well.

Obviously we can approximate circle curve with many line segments, and then just count their length. Obviously, all such line segments would be equal, we need to find just the length of one of them and multiply it by the total number of such tiny segments. If we break circle into 2^128 segments, then multiplying single segment length by 2^128 would be much faster than adding lengths of 2^128 segments (try it yourself).

We start with arrow pointing at 2 o'clock ([1,1] normalized), the we will gradually move it towards 0 o'clock [0,1]. Because we divide the length by 2 each time, at the end we need to multiply by power of two of the depth to which we ascended.

def sqrt(x): return math.sqrt(x)
def veclen(v): return sqrt(v[0]*v[0] + v[1]*v[1])
def vecadd(a,b): return [a[0]+b[0],a[1]+b[1]]
def vecsub(a,b): return [a[0]-b[0],a[1]-b[1]]
def vecmul(v,a): return [v[0]*a,v[1]*a]
def normalize(v):
l = veclen(v)
return [v[0]/l,v[1]/l]

def calc_pi(depth=100):
arrow=normalize([1.0,1.0])
for i in range(0,depth):
d = vecsub([0.0,1.0],arrow)
arrow = normalize(vecadd(arrow,vecmul(d,0.5)))
return veclen(vecsub([0.0,1.0],arrow))*2**depth*4

print calc_pi()


Yeah! Pi is indeed 3.141592...

I'm sure there are faster ways to calculate Pi, but the one I used follows directly from the definition of a circle.

Other, slower ways of calculate Pi:
1. Draw huge filled circle, using the same Pythagoras Theorem, count the number of pixels that make and divide.
2. Use central member of Pascal Triangle Pi=`2*(2^n / C(n,n/2))^2 / n`, that is very slow.
3. Use one of these obscure never converging series.

Name: Anonymous 2018-10-26 5:17

I've never ever calculated Pi and always trusted the value given online.
So lets check that Pi is actually 3.141592... For some reason, algorithm for calculating Pi is never given in programming textbook
So to calculate Pi, we need to devise our own algorithm, which requires precise understand what is Pi.
Duped again, sheeple. All hardware and software is compromised by Illuminati to prevent free-thinkers to discover the TRUE value of PI with which unmasks that we live on Flat Earth and that all science is a lie.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List