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 MVC Async vs Spring WebFlux
Java Program to Perform Inorder Recursive Traversal of a Given Binary Tree
Autoboxing và Unboxing trong Java
Java Program to implement Circular Buffer
Spring Boot - Internationalization
Java Program to Implement Binary Heap
Returning Image/Media Data with Spring MVC
Giới thiệu HATEOAS
A Guide to the finalize Method in Java
Database Migrations with Flyway
Java 9 Stream API Improvements
Spring MVC and the @ModelAttribute Annotation
RestTemplate Post Request with JSON
Spring Boot: Customize Whitelabel Error Page
Test a REST API with Java
MyBatis with Spring
How to Define a Spring Boot Filter?
Debug a HttpURLConnection problem
Java Program to Implement AA Tree
How to Read HTTP Headers in Spring REST Controllers
Quick Guide to the Java StringTokenizer
Introduction to the Functional Web Framework in Spring 5
Java Program to Implement Selection Sort
Performance Difference Between save() and saveAll() in Spring Data
Java – Write an InputStream to a File
Kết hợp Java Reflection và Java Annotations
Java Program to Implement Gauss Jordan Elimination
Adding Parameters to HttpClient Requests
Java Program to Implement Ternary Heap
Java Program to implement Bit Matrix
Convert Hex to ASCII in Java
Jackson – Marshall String to JsonNode