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:
Number Formatting in Java
Create Java Applet to Simulate Any Sorting Technique
Spring Boot - Bootstrapping
Java Program to Implement Patricia Trie
Marker Interface trong Java
Java Program to Implement Regular Falsi Algorithm
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Implement Horner Algorithm
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java Program to Implement Min Heap
Java 8 Predicate Chain
Java Program to Implement Double Ended Queue
LinkedList trong java
Java Program to Find a Good Feedback Vertex Set
Java Program to Compute Determinant of a Matrix
Java Program to Implement Disjoint Sets
Java Program to Implement Insertion Sort
Introduction to Spring Method Security
Runnable vs. Callable in Java
Java Program to Find the Longest Path in a DAG
Java Program to Implement Attribute API
Circular Dependencies in Spring
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Java Program to Find Strongly Connected Components in Graphs
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Decode a Message Encoded Using Playfair Cipher
Retrieve User Information in Spring Security
Comparing Two HashMaps in Java
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Jarvis Algorithm
Java Web Services – JAX-WS – SOAP
REST Web service: Basic Authentication trong Jersey 2.x