/**
 * Approximates the value of π using Euler's identity π = 4 - 4/3 + 4/5 - 4/7 + ...
 *
 * @author Drue Coles
 */
public class PiApproximator {

    public static void main(String[] args) {
        double π = 4.0;
        int denominator = 3;
        final int terms = 100_000_000;

        for (int i = 0; i < terms; i++) {

            // add or subtract next term
            if (i % 2 == 0) {
                π -= 4.0 / denominator;
            } else {
                π += 4.0 / denominator;
            }

            denominator += 2;
        }
        
        System.out.println("   Approximated value of π = " + π);
        System.out.println("Correct rounded value of π = " + Math.PI);
    }
}