This is a Java Program to Implement Cubic convergence 1/pi Algorithm. Cubic convergence is an algorithm used to calculate the value of 1/p.
Here is the source code of the Java Program to Implement Cubic convergence 1/pi Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/** ** Java Program to Implement Cubic convergence 1/pi Algorithm **/ import java.util.Scanner; /** Class CubicConvergencePi **/ public class CubicConvergencePi { /** compute 1/pi **/ public double getOneByPi(int k) { double ak = 1.0/3.0; double sk = (Math.sqrt(3) - 1)/2; double ak1, sk1, rk1; for (int i = 0; i < k; i++) { rk1 = 3.0 / (1 + 2 * Math.pow((1 - sk * sk * sk), (1.0/3.0))); sk1 = (rk1 - 1)/2.0; ak1 = rk1 * rk1 * ak - Math.pow(3, i) * (rk1 * rk1 - 1); ak = ak1; sk = sk1; } return ak; } /** Main function **/ public static void main (String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Cubic Convergence 1/Pi Algorithm Test\n"); /** Make an object of CubicConvergence class **/ CubicConvergencePi cc = new CubicConvergencePi (); System.out.println("Enter number of iterations"); int k = scan.nextInt(); System.out.println("\nValue of 1/pi : "+ cc.getOneByPi(k)); } }
Output:
Cubic Convergence 1/Pi Algorithm Test Enter number of iterations 3 Value of 1/pi : 0.3183098861837896
Related posts:
Creating a Generic Array in Java
Examine the internal DNS cache
Convert char to String in Java
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Login For a Spring Web App – Error Handling and Localization
A Guide to the Java ExecutorService
Java Program to Find Path Between Two Nodes in a Graph
Java – Try with Resources
Java Program to Implement AttributeList API
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Java Program to Implement Sparse Matrix
Apache Commons Collections Bag
Java Program to Implement Quick Sort with Given Complexity Constraint
Guide to @ConfigurationProperties in Spring Boot
Hướng dẫn Java Design Pattern – Iterator
Java Program to Implement the Bin Packing Algorithm
Converting Iterator to List
Weak References in Java
A Guide to ConcurrentMap
Java Program to Implement HashSet API
Spring Boot - Web Socket
Giới thiệu Google Guice – Injection, Scope
Filtering a Stream of Optionals in Java
Mix plain text and HTML content in a mail
Object cloning trong java
Why String is Immutable in Java?
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Java Program to Perform Deletion in a BST
Java Program to Implement Bloom Filter
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement Stack API
Simplify the DAO with Spring and Java Generics