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:
Giới thiệu thư viện Apache Commons Chain
Hướng dẫn Java Design Pattern – Transfer Object
Giới thiệu về Stream API trong Java 8
Implementing a Runnable vs Extending a Thread
Using the Not Operator in If Conditions in Java
Từ khóa throw và throws trong Java
Java Program to Check the Connectivity of Graph Using BFS
Java Program to Construct an Expression Tree for an Postfix Expression
Iterable to Stream in Java
Using a List of Values in a JdbcTemplate IN Clause
Hướng dẫn sử dụng String Format trong Java
Batch Processing with Spring Cloud Data Flow
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Control Table
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Guide to Mustache with Spring Boot
Integer Constant Pool trong Java
Java Program to add two large numbers using Linked List
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Toán tử trong java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement ConcurrentLinkedQueue API
Transaction Propagation and Isolation in Spring @Transactional
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Check for balanced parenthesis by using Stacks
Testing in Spring Boot
Java Program to Implement Graph Structured Stack
Java Program to Perform the Unique Factorization of a Given Number
Encode a String to UTF-8 in Java
Java Program to Implement Trie
Spring Boot - Servlet Filter