package chu_nz_baby;
import java.util.*;
import java.math.*;
/**
* <p>Title:π 圆周率的计算 </p>
* <p>Description:使用java BigDecimal完成圆周率(Chudnovsky公式)的计算。可以指定计算精度和计算参数。 </p>
* <p>Copyright: Copyright (c) 2004</p>
* <a href="http://www.jason314.com/palgorithm.htm">圆周率的计算方法#Chudnovsky公式</a>
* <a href="http://www.oursci.org/magazine/200301/030126.htm">圆周率π的计算历程</a>
* @author chu_nz_baby@hotmail.com
* @version 1.0
*/
public class Pi {
private BigDecimal result;
public Pi() {
}
public void run(int n, int scale) {
if(System.getProperty("debug") != null){
System.out.println("n=" + n);
}
BigDecimal upper = new BigDecimal(426880.0000 * Math.sqrt(10005.0000));
upper = upper.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
if(System.getProperty("debug") != null){
System.out.println("upper=" + upper);
}
BigDecimal downer = BigDecimal.valueOf(0);
for (int i = 0; i <= n; i++) {
BigDecimal d0 = factorial(6 * i).multiply ( new BigDecimal( 545140134 * i + 13591409));
BigDecimal d1 = factorial(i);
BigDecimal d2 = pow(d1, 3);
BigDecimal d3 = factorial(3 * i);
BigDecimal d4 = pow(-640320, 3 * i);
BigDecimal d5 = d2.multiply(d3);
BigDecimal d6 = d4.multiply(d5);
BigDecimal d = d0.divide( d6, BigDecimal.ROUND_HALF_EVEN );
downer = downer.add(d);
}
if(System.getProperty("debug") != null){
System.out.println("downer=" + downer);
}
if(System.getProperty("debug") != null){
System.out.println("upper.scale = " + upper.scale());
System.out.println("downer.scale = " + downer.scale());
}
result = upper.divide(downer, BigDecimal.ROUND_HALF_EVEN);
if(System.getProperty("debug") != null){
System.out.println("result.scale = " + result.scale());
}
}
public BigDecimal getResult() {
return result;
}
public