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 8 Stream API Analogies in Kotlin
Java Program to Implement Heap
Database Migrations with Flyway
Logout in an OAuth Secured Application
Java Program to implement Sparse Vector
Intro to the Jackson ObjectMapper
Spring Boot - Admin Client
Guide to @ConfigurationProperties in Spring Boot
Get and Post Lists of Objects with RestTemplate
Split a String in Java
Guide to Spring 5 WebFlux
Java Program to Implement WeakHashMap API
Send an email using the SMTP protocol
Changing Annotation Parameters At Runtime
Introduction to Spring MVC HandlerInterceptor
Spring @Primary Annotation
Spring Boot - Flyway Database
Dockerizing a Spring Boot Application
Một số nguyên tắc, định luật trong lập trình
Overflow and Underflow in Java
Simple Single Sign-On with Spring Security OAuth2
Tiêu chuẩn coding trong Java (Coding Standards)
The Basics of Java Security
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Hướng dẫn Java Design Pattern – Adapter
How to Add a Single Element to a Stream
Java Switch Statement
Java Program to Implement Heap Sort Using Library Functions
Java Program to Find Transpose of a Graph Matrix
Spring Webflux with Kotlin
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Xây dựng ứng dụng Client-Server với Socket trong Java