Open In App

Implementing Borwein Algorithm in Java

Last Updated : 02 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Borwein’s algorithm is a calculation contrived by Jonathan and Peter Borwein to compute the estimation of 1/Ï€. They conceived a few different algorithms. The following implementation of Borwein’s algorithm with quartic assembly in Java in fact ascertains Pi, however, converges excessively fast. In principle, a merges quartic ally to 1/Ï€. On every emphasis, the number of correct digits is in this way quadrupled.

Math library of java is used in implementing the Borwein algorithm which is the power and the root function of the library that is Math pow(). The java.lang.Math.pow() is used to calculate a number raised to the power of some other number. This function accepts two parameters and returns the value of the first parameter raised to the second parameter.

 In an implementation, from the Math module, two inbuilt functions are used as listed below:

  1. Power function: pow()
  2. Square root function: sqrt() 

1. pow() function 

Syntax:

public static double pow(double a, double b) ;

Parameter:

  • a: Base value whose power root is to be returned.
  • b: Exponent value to be returned.

Return: This method returns ab.

2. sqrt() function

Math sqrt() The java.lang.Math.sqrt() returns the square root of a value of type double passed to it as argument.

Syntax:

public static double sqrt(double a) ;

Parameter: The value whose square root is to be returned.

Return: This method returns the positive square root value of the argument passed to it.

Java




// Java program to implement Borwein Algorithm
  
// Importing generic java classes
import java.io.*;
  
class GFG {
  
    // Main driver method
    public double Borwein(int noOfIter)
    {
        // Calculating initial value of 1/pi
        double oneByPi = 6.0 - 4 * Math.sqrt(2);
  
        // Calculating the constant value y
        // used in Borwein Algorithm
        double y = Math.sqrt(2) - 1.0;
  
        double oneByPiAfterIter;
        double yAfterIter;
  
        // It calculates an estimation
        // of 1/pi that increases in accurary
        // the more iterations you use
        for (int i = 0; i < noOfIter; i++) {
  
            // Based on Algorithm formulas are used
            yAfterIter= (1 - Math.pow((1 - y * y * y * y), (0.25))) /
                          (1+ Math.pow((1 - y * y * y * y), (0.25)));
                            
            oneByPiAfterIter = oneByPi * Math.pow((1 + yAfterIter), 4) - 
            Math.pow(2, 2 * i + 3) * yAfterIter * (1 + yAfterIter + 
                                           yAfterIter * yAfterIter);
                                             
            y = yAfterIter;
              
            oneByPi = oneByPiAfterIter;
        }
        return oneByPi;
    }
  
    // Main driver method
    public static void main(String[] args)
    {
        // Object of above class in main
        GFG ob = new GFG();
  
        // Number of Iteration
        int noOfIter = 10;
  
        // Printing value of 1/pi
        System.out.println("Value of 1/pi : "
                        + ob.Borwein(noOfIter));
    }
}


Output:

Value of 1/pi : 0.31830988618379075


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads