- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathGaussLegendre.java
48 lines (41 loc) · 1.08 KB
/
GaussLegendre.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
packagecom.thealgorithms.others;
/**
* Guass Legendre Algorithm ref
* https://en.wikipedia.org/wiki/Gauss–Legendre_algorithm
*
* @author AKS1996
*/
publicfinalclassGaussLegendre {
privateGaussLegendre() {
}
publicstaticvoidmain(String[] args) {
for (inti = 1; i <= 3; ++i) {
System.out.println(pi(i));
}
}
staticdoublepi(intl) {
/*
* l: No of loops to run
*/
doublea = 1;
doubleb = Math.pow(2, -0.5);
doublet = 0.25;
doublep = 1;
for (inti = 0; i < l; ++i) {
double[] temp = update(a, b, t, p);
a = temp[0];
b = temp[1];
t = temp[2];
p = temp[3];
}
returnMath.pow(a + b, 2) / (4 * t);
}
staticdouble[] update(doublea, doubleb, doublet, doublep) {
double[] values = newdouble[4];
values[0] = (a + b) / 2;
values[1] = Math.sqrt(a * b);
values[2] = t - p * Math.pow(a - values[0], 2);
values[3] = 2 * p;
returnvalues;
}
}