javacodex.com
Java Examples
Java Examples
memu home

Leibniz Formula For PI

Leibniz Formula for PI (aka Gregory Leibniz Series):
   1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - 1/15 + 1/17 ... = pi/4

Source: (LeibnizFormula.java)

public class LeibnizFormula {
   public static void main(String[] args) {
 
      int count = 999999999;
      double pi = 0;
      double denominator = 1;
 
      for (int x = 0; x < count; x++) {
 
         if (x % 2 == 0) {
            pi = pi + (1 / denominator);
         } else {
            pi = pi - (1 / denominator);
         }
         denominator = denominator + 2;
      }
      pi = pi * 4;
      System.out.println(pi);
   }
}
 

Output:

# java LeibnizFormula 
3.1415926545880506

Contact: javacodex@yahoo.com