Advertisement
cd62131

Gauss–Legendre algorithm

Jan 27th, 2019
719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.62 KB | None | 0 0
  1. public class GaussLegendre {
  2.   public static void main(String[] args) {
  3.     double x1 = Double.MAX_VALUE;
  4.     for (double a0 = 1,
  5.             b0 = 1 / Math.sqrt(2),
  6.             t0 = 1. / 4,
  7.             p0 = 1,
  8.             x0 = (a0 + b0) * (a0 + b0) / 4 / t0,
  9.             a1,
  10.             b1,
  11.             t1,
  12.             p1;
  13.         Math.abs(x0 - x1) > Double.MIN_NORMAL;
  14.         a1 = (a0 + b0) / 2, b1 = Math.sqrt(a0 * b0), t1 = t0 - p0 * (a0 - a1) * (a0 - a1),
  15.             p1 = 2 * p0, a0 = a1, b0 = b1, t0 = t1, p0 = p1, x0 = x1,
  16.             x1 = (a0 + b0) * (a0 + b0) / 4 / t0) {}
  17.     System.out.println(x1);
  18.   }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement