Java Program to Implement Cubic convergence 1/pi Algorithm

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:

Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Nth Root Algorithm
Hướng dẫn sử dụng Printing Service trong Java
Write/Read cookies using HTTP and Read a file from the internet
How to Change the Default Port in Spring Boot
Từ khóa this và super trong Java
Spring Boot - Logging
Java Program to Perform Left Rotation on a Binary Search Tree
Functional Interfaces in Java 8
Java Program to Generate a Graph for a Given Fixed Degree Sequence
The Dining Philosophers Problem in Java
An Intro to Spring Cloud Vault
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Iterable to Stream in Java
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement K Way Merge Algorithm
Java Program to Generate N Number of Passwords of Length M Each
Java Program to Implement LinkedTransferQueue API
Stack Memory and Heap Space in Java
Java – InputStream to Reader
Java Program to Implement the Bin Packing Algorithm
A Guide to LinkedHashMap in Java
Java 8 and Infinite Streams
RegEx for matching Date Pattern in Java
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Kết hợp Java Reflection và Java Annotations
The StackOverflowError in Java
Java Program to Perform Insertion in a BST
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
Java Program to Represent Linear Equations in Matrix Form