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:
Spring Boot - Zuul Proxy Server and Routing
Java Program to Check Whether Graph is DAG
Hướng dẫn Java Design Pattern – Command
Immutable Map Implementations in Java
Jackson Date
Java Program to Implement Sorted Doubly Linked List
Java Program to Check the Connectivity of Graph Using DFS
Using JWT with Spring Security OAuth (legacy stack)
Giới thiệu HATEOAS
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Implement ConcurrentSkipListMap API
Java Copy Constructor
Adding Shutdown Hooks for JVM Applications
So sánh ArrayList và LinkedList trong Java
Explain about URL and HTTPS protocol
Spring Boot Configuration with Jasypt
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Solve a Matching Problem for a Given Specific Case
Custom Thread Pools In Java 8 Parallel Streams
Spring REST API with Protocol Buffers
Introduction to Spring MVC HandlerInterceptor
A Custom Data Binder in Spring MVC
Spring’s RequestBody and ResponseBody Annotations
Java Program to Perform the Sorting Using Counting Sort
Find the Registered Spring Security Filters
Java Program to Check for balanced parenthesis by using Stacks
Using Optional with Jackson
Spring WebFlux Filters
Command-Line Arguments in Java
Weak References in Java
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement Wagner and Fisher Algorithm for online String Matching