1 2 Previous Next 24 Replies Latest reply: Oct 16, 2007 2:09 AM by 800282 RSS

    Calculating Pi

    807587
      Hi all,

      I am writing a java program to calculate Pi from the following infinite serries

      pi=4*(1-1/3+1/5-1/7+1/9-1/11+1/13+...)

      I need clarifications as to how to implement this. I need to show the value of Pi
      approximated by term 1, term 2 etc as well as the difference between the approximate value
      and the actual value.

      // a method to perform this
      public double PI (long value)
      {
           double pi=0;
           int sign=1;
      
           for (int i=0; i < value; i++)
           {
                pi+= (double) // lost here     
           }
           return pi;
      }
      thanks.
        • 1. Re: Calculating Pi
          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
            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
              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
                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(total-Math.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
                  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
                    pi=4*(1-1/3+1/5-1/7+1/9-1/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
                      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
                        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
                          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
                            There might be faster converging series than the one
                            you chose. I'd do a Google search to see if that was
                            true.
                            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.

                            The fastest method is to call Math.PI of course. :)
                            • 11. Re: Calculating Pi
                              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
                                Hi duff....studying it..will get back should i encounter problems

                                Thanks
                                • 13. Re: Calculating Pi
                                  807587
                                  See this JavaOne presentation from Mr. Ronald Mak.

                                  [url http://servlet.java.sun.com/javaone/sf2003/conf/sessions/display-1991.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.apropos-logic.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
                                    There might be faster converging series than the one
                                    you chose. I'd do a Google search to see if that was
                                    true.
                                    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.
                                    1 2 Previous Next