简体   繁体   中英

Finding pi value using Leibniz series

I am starting to learn Java and I have a problem with my code.

For sure it has obvious mistakes: it doesn't run. I was asked to find the pi value using Leibniz series and also the number of iterations to reach six significant digit (3.141592).

So far i have this:

public class Findingpie2 {
    public static void main(String[] args) {
        double pi = 0.0;
        int counter = 1;
        for (int n = 0; n < counter; n++) {
            pi += Math.pow(-1, n) / (2*n + 1);
            counter++;
            if (pi==3.141592) {
                System.out.println("the value of pi is: "+String.format("%6f",4*pi));
                System.out.println("the number of iterations for pi value is "+n);
            }
        }
    }
}

Using only a tolerance criteria, displaying the result without any rounding:

package dummy;

import static java.lang.String.format;
import static java.lang.System.out;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Map.Entry;

/*
 * Finding pi value using Leibniz series
 * 
 * The Leibniz series is converging. To compare two successive values
 * is enough to get the required precision.
 * 
 * http://stackoverflow.com/questions/34834854/finding-pi-value-using-leibniz-serie
 * 
 */
public class Findingpie2 {

    public static void main(String[] args) {
        out.println("Pi, no rounding:");
        for (int i = 2; i < 10; i++) {
            double tolerance = Math.pow(0.1, i);
            Entry<Integer, Double> result = calcpi(tolerance);
            String pi = result.getValue().toString().substring(0,  i+1);
            out.println(format("The value of pi is: %s with %." + i + "f tolerance (%d iterations)." , pi, tolerance, result.getKey()));
        }
    }

    private static Entry<Integer, Double> calcpi(double tolerance) {
        int n = 0;
        double pi = 0;
        double bpi = 10 * tolerance;
        double inc = 1;
        while (Math.abs(bpi - pi) > tolerance) {
            bpi = pi;
            pi += inc / (2*n + 1);
            inc = -inc;
            n++;
        }
        return new SimpleImmutableEntry<Integer, Double>(n, 4 * pi);
    }

}

UPDATE : it'll will display:

Pi, no rounding:
The value of pi is: 3.1 with 0,01 tolerance (51 iterations).
The value of pi is: 3.14 with 0,001 tolerance (501 iterations).
The value of pi is: 3.141 with 0,0001 tolerance (5001 iterations).
The value of pi is: 3.1416 with 0,00001 tolerance (50001 iterations).
The value of pi is: 3.14159 with 0,000001 tolerance (500001 iterations).
The value of pi is: 3.141592 with 0,0000001 tolerance (5000001 iterations).
The value of pi is: 3.1415926 with 0,00000001 tolerance (50000001 iterations).
The value of pi is: 3.14159265 with 0,000000001 tolerance (499999987 iterations).

Use this instead:

public static void main(String[] args) {

          double pi = 0.0;
           int MAX_ITERATIONS=1000;
            int n=0;
            while(true) {
                pi += Math.pow(-1, n) / (2*n + 1);
                n++;
                if (n>MAX_ITERATIONS) {
                    System.out.println("the value of pi is: "+String.valueOf(4*pi));
                    System.out.println("the number of iterations for pi value is "+n);
                    break;
                }
            }
    }

and the result is:
the value of pi is:
the number of iterations for pi value is

Now, if you want to reach an exact precision, do like this:
Define an acceptable error, in your case, you need need to be 3.141592. Therefore, you need your tolerated error be less than 0.0000001. Change the above code as below:

    public static void main(String[] args) {
    double pi = 0.0;
    double ERROR = 0.0000001;
    int n=0;
    while(true) {
        pi += Math.pow(-1, n) / (2*n + 1);
        n++;
        if (Math.abs(4*pi-Math.PI)<ERROR) {
                    System.out.println("the value of pi is:        "+String.valueOf(4*pi));
                    System.out.println("the number of iterations for pi value is "+n);
                    break;
                }
            }
    }

And the result is :

the value of pi is: 3.1415919000000936
the number of iterations for pi value is 1326982

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM