# Calculating PI

Hey guys I have a open ended HW to calculate PI in a "interesting way" , wondering if any of you would have any ideas?

PI+e = delicious.
Thats just my two cents.

1 Like

isnt that 5.85987?

no idea really, according to my formula is says it is.
Raspberry + pi = dellicious.
Seriously if you wanna calculate some stupidly long value think of using raspberry pi as a platform cause of the low power draw.
It may take longer, but bang for bucks for that kind of calculation id say go for a low power solution.
Especially since ubuntu released their server for the platform.
You would be running a 4 core cpu, e.g. not a intel core i7 what ever, but you'd be burning 1/10th the wattage, and id guess 1/10th the price "atleast".
so unless you're in a hurry go pi.

neat idea for a side project but this is a HW for school and that is not an option.

HW as in HardWare?
how can you solve a problem using hardware alone?
Especially if it is a math problem.
Software solves math using hardware, hardware uses software to solve math, you cannot do one without the other.

Using what you left in the lounge...

``````bottom =3.0
pi = 1.0
for(var y=0;y<1000000000;y++){
if(y%2){/*If Its Odd*/
pi+=(1.0/bottom)
}else{/*If Its Even*/
pi-=(1.0/bottom)

}
bottom= bottom+2
}
console.log('PI is: ' + pi * 4)``````

in python...

``````bottom = 3
pi = 1

for y  in range(1000000):
if y%2 == 1:
pi += 1/bottom
else:
pi -= 1/bottom

bottom += 2

print("Pi is:", pi*4)``````

and in GO...

pi2:

``````package main

import (
"fmt"
"time"
)

func main() {
startTime := time.Now()
bottom := 3.0
pi := 1.0
iterations := 1000000000

for y := 0; y < iterations/2; y++ {
pi -= 1.0 / bottom
bottom = bottom + 2
pi += 1.0 / bottom
bottom = bottom + 2
}

pi = pi * 4

timeElapsed := time.Since(startTime)

fmt.Println("Time taken:\t", timeElapsed,
"\nPi is:\t\t", pi,
"\nTo\t\t", iterations, "iterations.")
}``````

pi:
(with concurrency, barely any idea what I'm doing here so it could be absolutely awful plsdonthitme)

``````package main

import (
"fmt"
"sync"
"time"
)

func pigen(n, its int, step, startBottom float64, pipe chan float64, wg *sync.WaitGroup) {
defer wg.Done()

partOfPi := 0.0
bottom := startBottom

if n%2 == 0 {
for y := 0; y < its; y++ {
partOfPi -= 1 / bottom
bottom = bottom + step
}
} else {
for y := 0; y < its; y++ {
partOfPi += 1 / bottom
bottom = bottom + step
}
}

pipe <- partOfPi
}

func main() {
startTime := time.Now()
pi := 1.0
iterations := 1000000000
routines := 8

pipe := make(chan float64, routines)

var wg sync.WaitGroup

step := float64(routines * 2)

for routine := 0; routine < routines; routine++ {
go pigen(routine, iterations/routines, step, float64(3+(routine*2)), pipe, &wg)
}

wg.Wait()
close(pipe)

for elem := range pipe {
pi += elem
}

pi = pi * 4

timeElapsed := time.Since(startTime)

fmt.Println("Time taken:\t", timeElapsed,
"\nPi is:\t\t", pi,
"\nTo\t\t", iterations, "iterations.")
}``````

comparing times Success kinda maybe I guess it worked doesn't really scale great really idk
8 routines only took like 2.5s off
idk why, I can't be bothered to look into it it's 1am all of a sudden.
Will have a look into it tomorrow if noone else's already come along by that point and gone "I found the problem it was really dumb ha ha you're an idiot".

2 Likes

Ever heard of Euler's formula?
Well identity in this case really.
e^(pi*i) + 1 =0

You can reverse engineer that to get pi. Complex (pun intended) but it's interesting and mathematically wonderful.

Oh and if you want to piss off your teacher denounce pi and use tau.
Ever notice how 2pi is more often used in math and science.

Here is a lovely read if you care.

I found some papers on the history of approximations of pi. It could at least guide you in the direction of the kind of algorithm you are looking for.
A short history of π formulas:
http://www.davidhbailey.com/dhbpapers/pi-history.pdf
The Quest for Pi:
http://www.davidhbailey.com/dhbpapers/pi-quest.pdf

Here is a more in depth look at, for example BBP which is used to calculate a sequence of numbers in pi from an arbitrary starting point, you have:
The BBP Algorithm for Pi:
http://www.davidhbailey.com/dhbpapers/bbp-alg.pdf

A lot of the more modern algorithms seem to have a quadratic, cubic or even quartic convergence to pi.
For example:

• Salamin-Brent (or Gauss–Legendre) algorithm (1976)
• Borwein's algorithm (1985)
• Chudnovskys' series (1987) similar to the Ramanujan's series, is an infinite series

But if you just want the first couple of hundred places you could use Machin's formula from 1706. Or if you want the first couple of million digits you might wanna look at Ramanujan's series (1916).

I don't know a lot about this, so please take this with a grain of salt. I just kind of glanced through the texts.

You can use a Taylor Series to derive pi.    There are others that converge faster though.

1 Like

I think the different answer I get from the concurrent alternative is to do with the order in which the values are added together because floats aren't 100% accurate to certain numbers so there'll be a margin of error for some of those reciprocals of "bottom", which will go a different way depending upon what they're being added to - which'll change depending upon how many routines I use!
Maybe
I think
Again, I'm not sure
please don't hit me if I'm wrong