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:
Spring Boot - Introduction
Spring Cloud – Securing Services
Convert XML to JSON Using Jackson
Java Program to Implement Find all Back Edges in a Graph
Encode/Decode to/from Base64
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Check Whether a Given Point is in a Given Polygon
How to Break from Java Stream forEach
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Create a Custom Auto-Configuration with Spring Boot
Java Program to Find the Vertex Connectivity of a Graph
Batch Processing with Spring Cloud Data Flow
Daemon Threads in Java
Spring Boot - Build Systems
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java – Write to File
Exception Handling in Java
Java Program to Implement Shell Sort
Java CyclicBarrier vs CountDownLatch
Java Program to Create a Balanced Binary Tree of the Incoming Data
A Guide to Concurrent Queues in Java
Generic Constructors in Java
Java Program to Represent Graph Using Incidence Matrix
Java Program to Implement Bloom Filter
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Check whether Graph is a Bipartite using DFS
Java 8 – Powerful Comparison with Lambdas
Default Password Encoder in Spring Security 5
Java Program to Implement Suffix Tree
A Guide to Java 9 Modularity
Intro to the Jackson ObjectMapper
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not