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:
- Power function: pow()
- 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
import java.io.*;
class GFG {
public double Borwein( int noOfIter)
{
double oneByPi = 6.0 - 4 * Math.sqrt( 2 );
double y = Math.sqrt( 2 ) - 1.0 ;
double oneByPiAfterIter;
double yAfterIter;
for ( int i = 0 ; i < noOfIter; i++) {
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;
}
public static void main(String[] args)
{
GFG ob = new GFG();
int noOfIter = 10 ;
System.out.println( "Value of 1/pi : "
+ ob.Borwein(noOfIter));
}
}
|
Output:
Value of 1/pi : 0.31830988618379075