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

Pages: 1-

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 3:49

Here's my version. It's just straightforward geometry so you can convince yourself that pi is 3.14.

from math import sqrt

class CircularTrack:
# equation for a circle: x^2 + y^2 = r^2

def __init__(self, x=0., y=0.):
self.x = x
self.y = y


def nudge_x_(self, delta_x):
self.x += delta_x
self.y = sqrt(1. - self.x**2)
return self


def distance_to(self, x, y):
return sqrt((x - self.x)**2 + (y - self.y)**2)


def approximate_pi(prec=4):
# Move from (0, 1) to (1, 0) summing the length of the tangent lines
c = CircularTrack(x=0., y=1.)
quadrant_length = 0.
while c.x <= (1. - 10**-prec):
x, y = c.x, c.y
c.nudge_x_(10**-prec)
quadrant_length += c.distance_to(x, y)
circumference = quadrant_length*4.
# π = circumference/2r
return circumference/2.

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.

Name: Anonymous 2018-10-26 6:04

package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(pi(5000))
}

func pi(n int) float64 {
ch := make(chan float64)
for k := 0; k <= n; k++ {
go term(ch, float64(k))
}
f := 0.0
for k := 0; k <= n; k++ {
f += <-ch
}
return f
}

func term(ch chan float64, k float64) {
ch <- 4 * math.Pow(-1, k) / (2*k + 1)
}

Name: Anonymous 2018-10-26 7:19

>>3
Halting problem makes it impossible to prevent arbitrary code execution, without forbidding access to turing-complete environment.

Name: Anonymous 2018-10-26 7:22

>>5
You clearly do not know what the halting problem is.

Name: Anonymous 2018-10-26 7:26

>>4
Ugly shit looks like Pascal.

Name: Anonymous 2018-10-26 7:27

>>6
You clearly do not know what the halting problem is.

Name: Anonymous 2018-10-26 7:36

>>8
You clearly do not know what the halting problem is.

Name: Anonymous 2018-10-26 7:36

>>7
Go (2018):
https://gobyexample.com/arrays
var a [5]int

Rust (2018):
https://doc.rust-lang.org/rust-by-example/primitives/array.html
let a: [i32; 500];

C (1972):
int a[5];

Name: Anonymous 2018-10-26 7:37

halt my dubs

Name: Anonymous 2018-10-26 7:38

>>10
so to create a 5-element array in Go and C you have to specify that it has 5 elements, but to create a 5-element array in Rust you have to say specify that it has 500?

Name: Anonymous 2018-10-26 8:34

>>4
Did you write this code? That's a pretty satisfying use of go channels.

Name: Anonymous 2018-10-26 8:38

>>9
You clearly do not know what the halting problem is.

Name: Anonymous 2018-10-26 10:42

>>14
You clearly do not know what the halting problem is.

Name: Anonymous 2018-10-26 13:12

Here is sqrt, if you don't trust library version:
def sqrt(x,depth=64):
x = float(x)
low = 0.0
high = max(x,1.0)
g = 1
for i in range(0,depth):
g = (high+low)/2
g2 = g*g
if g2>x: high = g
elif g2<x: low = g
else: return g
return g

Name: Anonymous 2018-10-26 15:21

>>16
Yeah, we all know sqrt is backdoored by NSA on hardware and library level.

Name: Anonymous 2018-10-26 15:27

>>17
You'll be SHOCKED when you find out what the Chinese have done!

Name: Buzzfeed 2018-10-26 16:03

>>18
Top 10 Crazy Hardware Backdoors (#3 Will Surprise You)

Name: Anonymous 2018-10-26 18:34

>>19
If your elliptic curve package uses CPU provided sqrt, then RF signal could be used to hijack it or other functions.

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