1、无穷级数计算 p = 1 - 1/3 + 1/5 -1/7+..... π=4p
2、使用 Nilakantha 级数 π = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + 4/(10*11*12) - (4/(12*13*14) .....
3、假设有一个圆半径为1,所以四分之一圆面积就为PI,而包括此四分之一圆的正方形面积就为1,
如果随意的在正方形中投射飞标(点)好了,则这些飞标(点)有些会落于四分之一圆内,假设所投射的飞标(点)有n点,在圆内的飞标(点)有c点,则依比例来算。
public class Main {
public static void main(String[] args) {
gailv();
}
//无穷级数计算 1 - 1/3 + 1/5 -1/7+.....
public static void wuqiongjishu() {
double p1 = 1, p2 = 0;
double i = 1;
double flag = 1;
double diff = Math.pow(0.1, 9);
while (Math.abs(p1 - p2) >= diff) {
i += 2;
flag = -flag;
p2 = p1;
p1 = p1 + flag / i;
}
System.out.println(p1 * 4);
}
public static void gailv() {
int N = (int)Math.pow(2,25);
double a[] = new double[N];
double b[] = new double[N];
for (int i = 0; i < N; i++) {
a[i] = Math.random();
b[i] = Math.random();
}
int cnt = 0;
for (int i=0;i<N;i++){
double c = a[i] * a[i] + b[i]*b[i];
if (c<=1){
cnt ++;
}
}
System.out.println(4.0*cnt/N);
}
}