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:
Life Cycle of a Thread in Java
Introduction to Thread Pools in Java
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Hướng dẫn Java Design Pattern – Dependency Injection
Sorting Query Results with Spring Data
Hướng dẫn Java Design Pattern – Transfer Object
Java Program to Implement Euler Circuit Problem
A Guide to @RepeatedTest in Junit 5
Guide to CopyOnWriteArrayList
New Features in Java 15
Java Program to Implement Fermat Primality Test Algorithm
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Implement Sieve Of Eratosthenes
Guide to Selenium with JUnit / TestNG
Java Program to Implement Levenshtein Distance Computing Algorithm
Java Program to Implement Regular Falsi Algorithm
A Comparison Between Spring and Spring Boot
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Auditing with JPA, Hibernate, and Spring Data JPA
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Spring Security Logout
Send email with JavaMail
Xử lý ngoại lệ trong Java (Exception Handling)
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Encode a Message Using Playfair Cipher
Java Program to Implement ArrayBlockingQueue API
Java Program to Find a Good Feedback Vertex Set
Jackson Annotation Examples
Spring 5 and Servlet 4 – The PushBuilder
Exception Handling in Java
Immutable Objects in Java