This content has been marked as final.
Show 24 replies

1. Re: Calculating Pi
807587 May 5, 2004 9:58 AM (in response to 807587)public class Test { public static double PI(long i) { double total = 0.0; int sign = 1; for (long j = 1; j <= i; j += 2) { total += sign / (double)j; sign *= 1; } return 4 * total; } public static void main(String [] args) { System.out.println(Test.PI(20000l)); } }

2. Re: Calculating Pi
807587 May 5, 2004 10:11 AM (in response to 807587)Thanks a lot Joey,
that was a very good one there. Now i need to get the no of terms of the serries that are needed to get
three significant digits of accuracy (3.14)
thank you. 
3. Re: Calculating Pi
798701 May 5, 2004 10:28 AM (in response to 807587)I guess you are supposed to plug in different values for the parameter "value" and try to find the smallest "value" for which the difference between the calculated value and the real value of pi is less than 0.005. Good luck! 
4. Re: Calculating Pi
807587 May 5, 2004 10:58 AM (in response to 798701)To reverse the logic, You could calculate the number of terms required to obtain a given error,// determine the number of terms required to calculate PI to within a given error. public static int PI(double error) { double total = 0.0; double sign = 4; int j; for (j = 1; Math.abs(totalMath.PI) > error; j += 2) { total += sign / j; sign =  sign; } return j; } public static void main(String[] args) { System.out.println(PI(0.005)); }

5. Re: Calculating Pi
807587 May 5, 2004 11:18 AM (in response to 807587)hum..thanks all for your inputs so far. Still trying to show a table like diagram (on paper) that indicates the value of PI approximated by term1, 2, etc the diff between the approximate value and the actual value.
But will like to get these from the program.
thanks. 
6. Re: Calculating Pi
807587 May 5, 2004 11:33 AM (in response to 807587)pi=4*(11/3+1/51/7+1/91/11+1/13+...)
Numerical note: due to a phenomenon known as loss of precision you'll get a more accurate result if you work out how many terms you need and then compute
4 * (... + 1/13  1/11 + 1/9  1/7 + 1/5  1/3 + 1) 
7. Re: Calculating Pi
807587 May 5, 2004 2:23 PM (in response to 807587)You will also get better/faster results by calculating two terms at once.public class Test { public static double PI(long i) { double total = 0.0; for (long j = 1; j <= i; j += 4) total += 1.0 / j  1.0 / (j+2); return 4 * total; } public static void main(String [] args) { System.out.println(Test.PI(20000l)); } }

8. Re: Calculating Pi
807587 May 5, 2004 2:25 PM (in response to 807587)Thanks to all...at least am making progress..will let u know the outcome. Not a easy one i guess 
9. Re: Calculating Pi
796254 May 5, 2004 2:30 PM (in response to 807587)There might be faster converging series than the one you chose. I'd do a Google search to see if that was true.
% 
10. Re: Calculating Pi
807587 May 5, 2004 2:33 PM (in response to 796254)There might be faster converging series than the one
There is a series which quadurples the number of digits of accurancy with each iteration. It was invented by an Indian in the 19th century who had no formal training in maths.
you chose. I'd do a Google search to see if that was
true.
The fastest method is to call Math.PI of course. :)

11. Re: Calculating Pi
796254 May 5, 2004 2:34 PM (in response to 796254)Look at this site:
http://mathforum.org/library/drmath/view/54456.html
It says that your series converges extremely slowly  10,000 terms to be correct to four digits.
1  1/3 + 1/5 1/7 + 1/9  1/11 +...
It suggests this:
1/2 + (1/2)*(1/3)*(1/2)^3 + ((1/2)*(3/4))*(1/5)*(1/2)^5 +
((1/2)*(3/4)*(5/6))*(1/7)*(1/2)^7 +...
This one looks quite complicated, but it converges to pi/6 pretty
quickly. The result of my program shows that I can get an answer
correct to ten digits by using only 15 terms in the series.
Look in Abramowitz and Stegun to see if they have the general expression for this series.
%

12. Re: Calculating Pi
807587 May 5, 2004 2:37 PM (in response to 796254)Hi duff....studying it..will get back should i encounter problems
Thanks 
13. Re: Calculating Pi
807587 May 5, 2004 2:43 PM (in response to 807587)See this JavaOne presentation from Mr. Ronald Mak.
[url http://servlet.java.sun.com/javaone/sf2003/conf/sessions/display1991.en.jsp]
BigInteger, BigDecimal, and a Billion Digits of Pi
The presentation is available in a PDF file ( http://servlet.java.sun.com/javaone/resources/content/sf2003/conf/sessions/pdfs/1991.pdf ) and the Java source files can be downloaded from the site http://www.aproposlogic.com/nc/
(site of the book "The Java Programmer's Guide to Numerical Computing")
The program is truly amazing  try it in your machine! It's very fast.

14. Re: Calculating Pi
DrClap May 5, 2004 3:22 PM (in response to 796254)There might be faster converging series than the one
As far as I know there are very few series that converge more slowly to pi than that one. But if that's your homework then that's what you do.
you chose. I'd do a Google search to see if that was
true.