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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** ** 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:
1 2 3 4 5 6 | Borwein 1 /Pi Algorithm Test Enter number of iterations 5 Value of 1 /pi : 0.31830988618379075 |
Related posts:
Java Program to Perform Insertion in a 2 Dimension K-D Tree
How to Manually Authenticate User with Spring Security
Immutable ArrayList in Java
Hamcrest Collections Cookbook
Java Program to Solve Knapsack Problem Using Dynamic Programming
Count Occurrences of a Char in a String
Java Program to Implement Graph Structured Stack
Guide to the Synchronized Keyword in Java
Java Program to Implement Jarvis Algorithm
A Guide to Java HashMap
Receive email using IMAP
Java program to Implement Tree Set
Guide to CountDownLatch in Java
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Implement Rolling Hash
Java Program to Implement Range Tree
Convert String to int or Integer in Java
A Quick Guide to Using Keycloak with Spring Boot
Spring @Primary Annotation
Java Convenience Factory Methods for Collections
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Solve TSP Using Minimum Spanning Trees
JUnit5 @RunWith
REST Web service: Basic Authentication trong Jersey 2.x
Remove All Occurrences of a Specific Value from a List
Java Program to Find Strongly Connected Components in Graphs
Introduction to the Java ArrayDeque
Java Program to Implement Binomial Heap
Spring Boot - Enabling Swagger2
Comparing Two HashMaps in Java
Mệnh đề Switch-case trong java
Creating a Custom Starter with Spring Boot