Calculating Pi

Posted By: Oxy

Calculating Pi - 05/05/08 20:36

Our infinite most beautiful number:

Here a way to calculate PI in Java.
(btw, LiteC is just using some different expressions for loops,
square-roots and float values)

 Code:
//calculating PI
		
		double sum=0;
		double adder=0;
		for (int i=0; i<2000000;i++)  //the more, the more exact
		{
			adder+=1;
			sum+=(1/(adder*adder));
		}
		
		sum = sum*6;
		sum=Math.sqrt(sum);  //quare root, the result is PI !!

		System.out.println("Pi is:"+sum);



gives us: 3.1415921761250813 (well its close)

Are there suggestions to calculate it quicker ?
Posted By: Joey

Re: Calculating Pi - 05/05/08 20:47

http://crd.lbl.gov/~dhbailey/bbp-formula.bmp gives you the n-th digit of pi without having to calculate the n-1 previous digits.
Posted By: Oxy

Re: Calculating Pi - 05/05/08 20:52

Ok, cool, thanks
Posted By: Joey

Re: Calculating Pi - 05/07/08 18:53

how does that work, by the way? i only see a function which diverges very quickly to pi.
Posted By: Oxy

Re: Calculating Pi - 05/07/08 21:45

The code I posted comes actually from Euler, who found out that the sum
of the infinite chain of natural numbers:

1/(1*1) + 1/(2*2) + 1/(3*3) ....

is exactly (PI*PI)/6

Thus we can calculate PI (using computers calculation power to get
the value of the infinite chain)
Posted By: Joey

Re: Calculating Pi - 05/08/08 08:07

i meant the formula i gave you.

have you tried 3 + 1/3 - 1/5 + 1/7 - 1/9...? this should be also faster than your approach.
© 2023 lite-C Forums