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 AMQP in Reactive Applications
Java Program to Represent Graph Using Incidence Matrix
Phân biệt JVM, JRE, JDK
Java 8 and Infinite Streams
Java – Reader to InputStream
Overflow and Underflow in Java
Versioning a REST API
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Spring’s RequestBody and ResponseBody Annotations
Form Validation with AngularJS and Spring MVC
Adding Shutdown Hooks for JVM Applications
Introduction to Spring Data JDBC
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Find Nearest Neighbor Using Linear Search
Hướng dẫn Java Design Pattern – Visitor
Create Java Applet to Simulate Any Sorting Technique
Java Program to Find Path Between Two Nodes in a Graph
Java Program to Check whether Undirected Graph is Connected using DFS
An Intro to Spring Cloud Zookeeper
Java Program to Implement Find all Forward Edges in a Graph
A Guide to Queries in Spring Data MongoDB
Java Program to Describe the Representation of Graph using Adjacency List
Spring Boot - Tomcat Deployment
An Intro to Spring Cloud Task
A Guide to ConcurrentMap
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Perform Insertion in a BST
Tips for dealing with HTTP-related problems
How to Kill a Java Thread
Java Program to Implement Euler Circuit Problem
Lớp LinkedHashMap trong Java
Spring Boot - File Handling