This is a Java Program to Implement Borwein Algorithm. Borwein’s algorithm is an algorithm devised by Jonathan and Peter Borwein to calculate the value of 1/π.
Here is the source code of the Java Program to Implement Borwein Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to Implement Borwein Algorithm
**/
import java.util.Scanner;
/** Class Borwein **/
public class Borwein
{
/** compute 1/pi **/
public double getOneByPi(int k)
{
double ak = 6.0 - 4 * Math.sqrt(2);
double yk = Math.sqrt(2) - 1.0;
double ak1 ;
double yk1 ;
for (int i = 0; i < k; i++)
{
yk1 = (1 - Math.pow((1 - yk * yk * yk * yk),(0.25)))/(1 + Math.pow((1 - yk * yk * yk * yk),(0.25)));
ak1 = ak * Math.pow((1 + yk1), 4) - Math.pow(2, 2 * i + 3) * yk1 * (1 + yk1 + yk1 * yk1);
yk = yk1;
ak = ak1;
}
return ak;
}
/** Main function **/
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Borwein 1/Pi Algorithm Test\n");
/** Make an object of Borwein class **/
Borwein b = new Borwein();
System.out.println("Enter number of iterations ");
int k = scan.nextInt();
System.out.println("\nValue of 1/pi : "+ b.getOneByPi(k));
}
}
Output:
Borwein 1/Pi Algorithm Test Enter number of iterations 5 Value of 1/pi : 0.31830988618379075
Related posts:
Java Program to Implement Suffix Tree
Java Program to Implement EnumMap API
Array to String Conversions
Tips for dealing with HTTP-related problems
Hướng dẫn Java Design Pattern – Composite
The Guide to RestTemplate
Guide to the Java Queue Interface
Converting Iterator to List
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Perform the Shaker Sort
Zipping Collections in Java
Introduction to Java Serialization
Debugging Reactive Streams in Java
Using Spring ResponseEntity to Manipulate the HTTP Response
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Send email with authentication
Guide to Selenium with JUnit / TestNG
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Guide to java.util.concurrent.Locks
HttpClient 4 – Send Custom Cookie
Java Program to Implement Affine Cipher
Composition, Aggregation, and Association in Java
A Guide to Java SynchronousQueue
Vòng lặp for, while, do-while trong Java
Check If a File or Directory Exists in Java
Java – InputStream to Reader
Entity To DTO Conversion for a Spring REST API
New Features in Java 15
Lớp Arrarys trong Java (Arrays Utility Class)
How to Store Duplicate Keys in a Map in Java?
Java Program to Generate Randomized Sequence of Given Range of Numbers
Spring Boot Tutorial – Bootstrap a Simple Application