title: calculate Pi in java (fast)
body: <p>I want to make a fast generator (if possible, in Java) which gives me Pi (with decimals of course).
  I have made one already, which uses a for loop and BigDecimals:  </p>
  
  <pre><code>BigDecimal pi;
      pi = new BigDecimal("0");
      BigDecimal divide = new BigDecimal("1");
      long max = &lt;Any Number&gt;;
  
          for (long l = 1; l &lt; max; l++) {
  
          BigDecimal two = new BigDecimal("2"); //it gave me an error if it wasn't a BigDecimal.
          BigDecimal four = new BigDecimal("4");
          BigDecimal divide2 = divide.add(two);
          BigDecimal divideend = four.divide(divide, 50, BigDecimal.ROUND_HALF_UP);
          BigDecimal divideend2 = four.divide(divide2, 50, BigDecimal.ROUND_HALF_UP);
          pi = pi.add(divideend);
          pi = pi.subtract(divideend2);
          System.out.println(pi.toString() + "                   " + i + " of " + max);
          divide = divide.add(vier);
          }
      System.out.println(pi.toString() + "      finished.");
  </code></pre>
  
  <p>the problem with this is:<br>
  I wanted to let it run for 500.000.000 times (500 Million), it took just over 5 hours and had "only" about 12 correct decimal places... is there any faster solution?  </p>
  
  <p>EDIT: You don't have to help me anymore, I already have a solution (If anyone else needs that too: here it is)  </p>
  
  <pre><code>import java.math.BigDecimal;
  
  public class Pitest3 {
  
  public static void main(String[] args) {
  
      BigDecimal pi = new BigDecimal("3");
      long l = 1l;
      long max = 100000; //How often, the more, the more exact
      BigDecimal axbxc;
      BigDecimal divide1;
      BigDecimal divide2;
      BigDecimal divide3;
      divide1 = new BigDecimal("2");
      divide2 = new BigDecimal("3");
      divide3 = new BigDecimal("4");
      BigDecimal four = new BigDecimal("4");
      BigDecimal sumthing1;
      BigDecimal sumthing2;
      BigDecimal two = new BigDecimal("2");
      BigDecimal sumthing3;
      BigDecimal sumthing4;
  
  
      while (l &lt; max) {
  
          sumthing1 = divide1.multiply(divide2);
          axbxc = sumthing1.multiply(divide3);
  
          sumthing2 = four.divide(axbxc, 100, BigDecimal.ROUND_HALF_UP);
  
          pi = pi.add(sumthing2);
  
          divide1 = divide1.add(two);
          divide2 = divide2.add(two);
          divide3 = divide3.add(two);
  
          sumthing3 = divide1.multiply(divide2);
          axbxc = sumthing3.multiply(divide3);
  
          sumthing4 = four.divide(axbxc, 100, BigDecimal.ROUND_HALF_UP);
  
          pi = pi.subtract(sumthing4);
  
          divide1 = divide1.add(two);
          divide2 = divide2.add(two);
          divide3 = divide3.add(two);
  
          System.out.println(pi.toString() + "            " + l + " of " + max)
  
          l++;
  
  
  
      }
  
      System.out.println(pi.toString() + "   finished!");
  
  }
  </code></pre>
  
  <p>}</p>