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:
How to Manually Authenticate User with Spring Security
Java Program to Implement Fermat Primality Test Algorithm
Transaction Propagation and Isolation in Spring @Transactional
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Perform String Matching Using String Library
Comparing Arrays in Java
Using the Not Operator in If Conditions in Java
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Spring Boot - Unit Test Cases
A Guide to the Java LinkedList
Getting a File’s Mime Type in Java
Hướng dẫn Java Design Pattern – MVC
Bootstrapping Hibernate 5 with Spring
A Guide to Spring Boot Admin
Allow user:password in URL
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Guide to Java Instrumentation
@Lookup Annotation in Spring
Collect a Java Stream to an Immutable Collection
An Intro to Spring Cloud Task
Java Program to Implement Nth Root Algorithm
Hướng dẫn Java Design Pattern – Iterator
Lớp Collections trong Java (Collections Utility Class)
Java Program to Implement Ternary Heap
Feign – Tạo ứng dụng Java RESTful Client
Versioning a REST API
Redirect to Different Pages after Login with Spring Security
Receive email by java client
Intro to the Jackson ObjectMapper
Java Program to Implement Double Order Traversal of a Binary Tree
Spring Boot - Application Properties
Create a Custom Exception in Java