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 Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Java Program to Implement Nth Root Algorithm
Command-Line Arguments in Java
Spring Data JPA @Query
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
What is Thread-Safety and How to Achieve it?
Java Convenience Factory Methods for Collections
The XOR Operator in Java
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Returning Custom Status Codes from Spring Controllers
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Implement Ternary Search Tree
How to Remove the Last Character of a String?
Java Scanner hasNext() vs. hasNextLine()
Java Program to Implement DelayQueue API
Hướng dẫn Java Design Pattern – Adapter
ArrayList trong java
Java Program to Implement Shell Sort
Java Program to Implement JobStateReasons API
Java InputStream to Byte Array and ByteBuffer
Java Program to Implement Booth Algorithm
Java IO vs NIO
Java Program to Implement Threaded Binary Tree
Join and Split Arrays and Collections in Java
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Java Program to Find Minimum Element in an Array using Linear Search
HashMap trong Java hoạt động như thế nào?
How to Use if/else Logic in Java 8 Streams
Concrete Class in Java
Map Interface trong java
Java Program to Implement Find all Cross Edges in a Graph