Files
Java/src/main/java/com/thealgorithms/others/GuassLegendre.java
vaibhav9t1 9eaa2bb756 style: enable MultipleVariableDeclarations in checkstyle (#5175)
Co-authored-by: vaibhav <vaibhav.waghmare@techprescient.com>
2024-05-25 20:18:27 +02:00

49 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.thealgorithms.others;
/**
* Guass Legendre Algorithm ref
* https://en.wikipedia.org/wiki/GaussLegendre_algorithm
*
* @author AKS1996
*/
public final class GuassLegendre {
private GuassLegendre() {
}
public static void main(String[] args) {
for (int i = 1; i <= 3; ++i) {
System.out.println(pi(i));
}
}
static double pi(int l) {
/*
* l: No of loops to run
*/
double a = 1;
double b = Math.pow(2, -0.5);
double t = 0.25;
double p = 1;
for (int i = 0; i < l; ++i) {
double[] temp = update(a, b, t, p);
a = temp[0];
b = temp[1];
t = temp[2];
p = temp[3];
}
return Math.pow(a + b, 2) / (4 * t);
}
static double[] update(double a, double b, double t, double p) {
double[] values = new double[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;
return values;
}
}