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 Treap
Hướng dẫn Java Design Pattern – Service Locator
How to Read a File in Java
Spring @RequestParam Annotation
Java IO vs NIO
Beans and Dependency Injection
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Convert String to Byte Array and Reverse in Java
Stack Memory and Heap Space in Java
Apache Tiles Integration with Spring MVC
Spring Boot Gradle Plugin
Login For a Spring Web App – Error Handling and Localization
Compare Two JSON Objects with Jackson
Guide to Apache Commons CircularFifoQueue
Spring Boot - Quick Start
Java Program to Implement Leftist Heap
Removing all duplicates from a List in Java
Convert Hex to ASCII in Java
Java Program to Implement RoleUnresolvedList API
Spring Boot - Tomcat Port Number
Adding Parameters to HttpClient Requests
Chương trình Java đầu tiên
Java 9 Stream API Improvements
Java Program to Implement Adjacency Matrix
Netflix Archaius with Various Database Configurations
Java Program to Implement Merge Sort Algorithm on Linked List
Toán tử trong java
Java 8 StringJoiner
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program to Implement Hash Tables with Linear Probing
Java Program to Implement Vector API
Java Program to implement Array Deque