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:
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Fixing 401s with CORS Preflights and Spring Security
How to Add a Single Element to a Stream
SOAP Web service: Authentication trong JAX-WS
Send email with authentication
Java Program to find the maximum subarray sum using Binary Search approach
Guide to Spring Cloud Kubernetes
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Using JWT with Spring Security OAuth
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Implement CopyOnWriteArrayList API
A Guide to JPA with Spring
HttpClient Connection Management
Java Program to Implement Binary Heap
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Converting Strings to Enums in Java
Java Program to Perform Search in a BST
Spring REST API + OAuth2 + Angular
Java Program to Implement LinkedHashSet API
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Introduction to Spring Cloud Netflix – Eureka
Java Program to Implement Knapsack Algorithm
Bootstrap a Web Application with Spring 5
Prevent Cross-Site Scripting (XSS) in a Spring Application
Java Program to Implement Binomial Tree
Jackson – Marshall String to JsonNode
LIKE Queries in Spring JPA Repositories
A Custom Data Binder in Spring MVC
Redirect to Different Pages after Login with Spring Security
A Guide To UDP In Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Path